Rock paper scissors wont work

okay… how can i fix my indentation

Here is an example:

const myFunction = (param) => { //opening brace 
  if(param == something) { //opening brace
    if(this == that) { //opening brace
      //some code
    } //closing brace indented the same as the left side of the nested if
  } //closing if block brace indented the same as the left side of the if it belongs to
} //myFunction() closing brace indented the same as the left side of the line defining this block

//Another example without comments
const someFunction = () => {
  if(something) {
    if(something else) {
      //code
    } else if (some other condition) {
      //code
    } else {
      //code
    }
  }
}

Keep in mind, the indentation won’t prevent code from running or throw errors, but cleaning it up will make it much easier to spot the problems with your curly braces.

Opinion

If a function is intended to have long term use then give it a slice of the namespace, and some of the functionality it commands.

const foo = function () {

}

What’s the difference between that, and this?

const foo = () => {

}

where are the misplaced curly braces

you mean how the curly braces are placed affects my code

i fixed my indentation. now it says that my playGame function is not defined.

const getUserChoice = (userInput) => {
  userInput = userInput.toLowerCase();
  if(userInput === 'rock' || userInput === 'paper' || userInput === 'scissors') {
    return userInput;
  	} else {
    console.log("error")
  	} 
	};
getUserChoice('rock');

const getComputerChoice = () => { 
  const randomNumber =
Math.floor(Math.random() * 3);
  switch(randomNumber) { 
    case 0:
      return 'rock';
    case 1:
      return 'paper';
    case 2:
      return 'scissors';
  	}
	}    

const determineWinner = (userChoice, computerChoice) => {
{ if (userChoice === computerChoice)
    return 'Tie!';
	}
if (userChoice === 'rock') {
 if (computerChoice === 'paper') {
   return 'You lose';
   } else if (userChoice === 'rock' && computerChoice === 'scissors') {
   return 'you won!';
 	}
}
if (userChoice === 'scissors') {
  if (computerChoice === 'paper') {
    return 'you win';
   } else if (userChoice === 'scissors' && computerChoice === 'rock') {
   return 'you lose'
 	}
}  
if (userChoice === 'paper') {
  if (computerChoice === 'rock') {
    return 'you win';
  } else if (userChoice === 'paper' && computerChoice === 'scissors') {
    return 'you lose';
	}
}
const playGame = () => {
 const userChoice = getUserChoice('rock');
  const computerChoice = getComputerChoice();
  console.log('You threw: ' + userChoice);
  console.log('The computer threw: ' + computerChoice);
  console.log(determineWinner(userChoice, computerChoice));
	}
};
playGame();

i fixed it…

I’m glad you got it working. Just because I spent a fair amount of time fixing the indentation of your original post, I’ll share it with you. It contains comments for everything I moved/added.

Spoiler
const getUserChoice = (userInput) => {
  userInput = userInput.toLowerCase();
  if(userInput === 'rock' || userInput === 'paper' || userInput === 'scissors') {
    return userInput;
  } else {
    console.log("error")
  }// moved left
} //removed semicolon

//getUserChoice('rock'); //this line should be deleted
const getComputerChoice = () => { 
  const randomNumber =
Math.floor(Math.random() * 3);
  switch(randomNumber) { 
    case 0:
      return 'rock';
    case 1:
      return 'paper';
    case 2:
      return 'scissors';
  }// moved right
}// moved left
    
const determineWinner = (userChoice, computerChoice) => {
  if (userChoice === computerChoice) { //this brace was missing
    return 'Tie!';
  }// moved right
  if (userChoice === 'rock') { //moved this line right
    if (computerChoice === 'paper') { //moved this line right
      return 'You lose'; //moved this line right
    } else if (userChoice === 'rock' && computerChoice === 'scissors') { //moved whole line right
      return 'you won!'; //moved this line right
    }// moved right
  }// moved right
  if (userChoice === 'scissors') {//moved this line right
    if (computerChoice === 'paper') {// moved this line right
      return 'you win';// moved this line right
    } else if (userChoice === 'scissors' && computerChoice === 'rock') {// moved whole line right
      return 'you lose'// moved this line right
    }// moved right
  }// moved right  
  if (userChoice === 'paper') {// moved this line right
    if (computerChoice === 'rock') {// moved this line right
      return 'you win';// moved this line right
    } else if (userChoice === 'paper' && computerChoice === 'scissors') {// moved this whole line right
      return 'you lose';// moved this line right
    }// moved right
  }// moved right
}// this brace was missing to end the determineWinner() block

const playGame = () => {
  const userChoice = getUserChoice('rock'); //moved line right
  const computerChoice = getComputerChoice();
  console.log('You threw: ' + userChoice);
  console.log('The computer threw: ' + computerChoice);
  console.log(determineWinner(userChoice, computerChoice));
} //no semicolon needed here

playGame();

Happy coding!

Aside

The indentation has no meaning to the interpreter, just the reader. We don’t even need indentation, just clarity.

To get clarity we give all closing braces their own line, and all opening braces stay on the line of their signatures. That’s at least one way of thinking. Another would be to give ALL braces, whether opening or closing their own line.

Take your code for example. Remove all the indentation and apply the above rule. Be sure to count backwards from every closing brace to be sure it has a corresponding opening brace.

We will sometimes see the terms LBRACE and RBRACE. By now we know what the terms mean, so if you ever see these terms you won’t be put off.

{  =>  LBRACE
}  =>  RBRACE

Once you have your code written as described above and it is running as expected, Go through and consider where indentation would help you in being able to understand and debug, if necessary, your code. Gain an appreciation from this exploration and you are doubly the winner. One for having done it, and two for having gained from it.

Hey guys,

Rather than making another thread I thought I’d try posting my problem here as it seems similar.
Now it may just be down to tiredness but could someone take a quick peek at my code below to see where I’m going wrong. I keep getting an ‘undefined’ when trying to call the my function and I can’t quite figure out why so fresh eyes are needed.

Thank you in advance.

const getUserChoice = userInput => {
  var userInput = userInput.toLowerCase();
  if (userInput === 'rock' || userInput === 'scissors' || userInput === 'paper'){
    return userInput;
      } else {
    console.log("Error. Choice was not rock, scissors or paper. Please, input a valid choice.")
  }
};

const getComputerChoice = () => {
  const randomNumber = (Math.floor(Math.random() * 3));
  switch(randomNumber){
    case 0:
      return 'rock';
    case 1:
      return 'paper';
    case 2:
      return 'scissors';
  }
};

const determineWinner = (userChoice, computerChoice) => {
  if (userChoice === computerChoice) {
    return "The game is a tie.";
  }
  if (userChoice === 'rock ') {
    if (computerChoice === 'paper') {
      return 'The computer won!';
    } else {
      return 'You won!';
    }
  }
  if (userChoice === 'paper') {
    if (computerChoice === 'scissors') {
      return 'The computer won!';
    } else {
      return 'You won!';
    }
  }
  if (userChoice === 'scissors') {
    if(computerChoice === 'rock') {
      return 'The computer won!';
    } else {
      return 'You won!';
    }
  }
};

console.log(determineWinner('rock', 'scissors'));
if (userChoice === 'rock ')

You have a space after 'rock' within the string compare condition. This causes the undefined.

But next time please do open a new thread. Otherwise these threads will get longer and longer…

@alexcraig could you close this topic?

1 Like