Melanie Sigrid
Melanie Sigrid

Reputation: 15

How can I make my function return a value other than undefined?

I am trying to complete the Rock-Paper-Scissors challenge from The Odin Project. I am stuck; I don't know why when I run my code, my function playRound returns undefined. Any help would be appreciated.

  const computerPlay = () => { //Randomly returns 'Rock', 'Paper', or 'Scissors'
  let choices = ['rock', 'paper', 'scissors'];
  let computerChoice = choices[Math.floor(Math.random() * choices.length)];
  return computerChoice;
}

const playRound = (playerSelection, computerSelection) => {
  playerSelection = playerSelection.toLowerCase;
  if (playerSelection === computerSelection) {
    return "It's a tie";
  } else {
    switch (playerSelection.toLowerCase) {
      case 'rock':
        switch (computerSelection) {
          case 'paper':
            return "You Lose! Paper beats Rock";
          case 'scissors':
            return "You Win! Rock beats Scissors";
        }
      case 'paper':
        switch (computerSelection) {
          case 'rock':
            return "You Win! Paper beats Rock";
          case 'scissors':
            return "You Lose. Scissors beats Paper"
        }
      case 'scissors':
        switch(computerSelection) {
          case 'rock':
            return "You Lose. Rock beats scissors";
          case 'paper':
            return "You Win. Scissors beats Paper"
        }
      }

  }
}

const playerSelection = "rock";
const computerSelection = computerPlay();
console.log(playRound(playerSelection, computerSelection));

Upvotes: 1

Views: 80

Answers (3)

Adrian
Adrian

Reputation: 26

const sentence = 'My Dog Is Brown';

console.log(sentence.toLowerCase());
// expected output: "my dog is brown"

The .toLowerCase() method returns the value of the string converted to lower case.
.toLowerCase() does not affect the value of the string itself.

Upvotes: 0

s.d.fard
s.d.fard

Reputation: 301

You are missing parentheses -> .toLowerCase()

Upvotes: 0

ahsan
ahsan

Reputation: 1504

You're missing parentheses on toLowerCase method.

 const computerPlay = () => { //Randomly returns 'Rock', 'Paper', or 'Scissors'
  let choices = ['rock', 'paper', 'scissors'];
  let computerChoice = choices[Math.floor(Math.random() * choices.length)];
  return computerChoice;
}

const playRound = (playerSelection, computerSelection) => {
  playerSelection = playerSelection.toLowerCase();
  if (playerSelection === computerSelection) {
    return "It's a tie";
  } else {
    switch (playerSelection.toLowerCase()) {
      case 'rock':
        switch (computerSelection) {
          case 'paper':
            return "You Lose! Paper beats Rock";
          case 'scissors':
            return "You Win! Rock beats Scissors";
        }
      case 'paper':
        switch (computerSelection) {
          case 'rock':
            return "You Win! Paper beats Rock";
          case 'scissors':
            return "You Lose. Scissors beats Paper"
        }
      case 'scissors':
        switch(computerSelection) {
          case 'rock':
            return "You Lose. Rock beats scissors";
          case 'paper':
            return "You Win. Scissors beats Paper"
        }
      }

  }
}

const playerSelection = "rock";
const computerSelection = computerPlay();
console.log(playRound(playerSelection, computerSelection));

Upvotes: 3

Related Questions