Search code examples
javascriptfunctionswitch-statementundefined

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));

Solution

  • 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));