const getUserChoice = userInput => {
userInput = userInput.toLowerCase();
if (userInput === 'rock' || userInput === 'paper' || userInput === 'scissors') {
return userInput;
} else {
console.log('Please play Rock Paper Scissors');
}
};
function getComputerChoice() {
let choice = Math.floor(Math.random() * 3);
switch (choice) {
case 0 :
return 'rock';
break;
case 1 :
return 'paper';
break;
case 2 :
return 'scissors';
break;
}
};
function determineWinner(userChoice, computerChoice) {
if (userChoice === computerChoice) {
return 'The game is a tie!';
}
if (userChoice === 'rock') {
if (computerChoice === 'paper') {
return 'you lose.';
} else {
return 'you win!';
}
if (userChoice === 'paper') {
if (computerChoice === 'scissors') {
return 'you lose.';
} else {
return 'you win!';
}
if (userChoice === 'scissors') {
if (computerChoice === 'rock') {
return 'you lose.';
} else {
return 'you win!';
};
console.log(determineWinner('rock', 'paper'));
Generally, it’s quite useful if you also provide the error which you’re getting - they often contain quite useful information, like precisely what the interpreter has taken issue with in your code and where it happened.
In lieu of this, I’ve run your code and I get this error:
index.js:50
console.log(determineWinner('rock', 'paper'));
SyntaxError: Unexpected end of input
This is… not the most helpful error, admittedly.
If you’re working in the learning environment, it should largely be helping you out with indentation and parentheses pairing to help you spot these… but you might want to check whether all your curly braces are where they ought to be.
Hint
You’re missing several, and the “unclosed” pairs are what is causing your error.
Thank You,
I seem to be a little careless with semi-colons. Is there any way to be less sluggish and implement them more precisely, so mistakes like this won’t happen again?
best LT
Fun fact for you: while you’re meant to end every statement in JS with a semicolon, the interpreter won’t actually complain if you miss them…
let example = "This is a string"
console.log(example)
console.log("No semi-colons here!!")
As to precision, that’s largely on you. Code editors will try and help you out by automatically generating parentheses in pairs or making auto-complete suggestions, but these are all aids intended to make your coding life easier and are not a substitute for paying attention to what you’re doing.
That being said, and this is less useful for the learning environment but will help when you’re on your own, there are a plethora of code formatters available - for example, VSCode has Beautify or Prettier for JavaScript.
The code formatter, when applied to your code, will rearrange it to meet some pre-determined “specification” of how your code should be laid out. If the formatter thinks that indentation should be multiples of 4 spaces, it’ll make it so etc. Tools like that will make it easier for you to notice if you’ve missed a closing bracket or similar, because they won’t line up.
This topic was automatically closed 41 days after the last reply. New replies are no longer allowed.