When writing the code below I can’t leave the argument for console.log(getUserChoice()); empty.
The error message says that the .toLowerCase() property can’t be used at a undefined value.
What’s wrong, I want the code to print out the else-statement ‘Enter a valid choise’.
Further on, I also would like to know what’s wrong when I only get the message ‘Tie’? I tried to look at the recent post but it doesn’t seem to be the same reason why the code doesn’t work for me.
You apply the toLowerCase method to an undefined variable before you get to the if … else condition. So the error is thrown before you can reach the else statement. Once an error is thrown, the code stops executing.
If you intend to have an optional parameter, set a default value like so:
Thank you for answering and pointing out the guidelines. I actually think I asked the same thing last time I tried to get into this subject more seriously.
Is it possible to use some other solution for the first issue, because the “?” isn’t mentioned in the hint you get?
const getComputerChoice = () => {
let randomNr = Math.floor(Math.random() * 3);
if (randomNr === 0) {
return 'Rock';
} else if (randomNr === 1) {
return 'Paper';
} else {
return 'Scissors';
}
}
console.log(getComputerChoice());
const determineWinner = (userChoice, computerChoice) => {
if (userChoice === computerChoice) {
return '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());
I’m not sure if I understand the question, but I think you are referring to the Optional chaining operator? I edited my post and made another suggestion as the chaining parameter doesn’t seem to work in every sandbox.
To the 2nd issue:
determineWinner expects an argument
Thanks for taking time to answer. I just read that they (CA) will make som changes to their content.
I might use a somewhat slower pace until the changes are implemented.
Hello,
I’m stuck on step 5 of this project. I’m putting in the code:
Const getComputerChoice = () => {
Const randomNumber = Math.floor(Math.random() * 3);
Switch (randomNumber) {
case 0:
return ‘rock’;
case 1:
return ‘paper’;
case 2:
return ‘scissors’;
I’m getting the following error:/home/ccuser/workspace/javascript_101_Unit_3/Unit_3/rockPaperScissors.js:10
Const getComputerChoice = () => {
^^^^^^^^^^^^^^^^^
SyntaxError: Unexpected identifier
at createScript (vm.js:53:10)
at Object.runInThisContext (vm.js:95:10)
at Module._compile (module.js:543:28)
at Object.Module._extensions…js (module.js:580:10)
at Module.load (module.js:488:32)
at tryModuleLoad (module.js:447:12)
at Function.Module._load (module.js:439:3)
at Module.runMain (module.js:605:10)
at run (bootstrap_node.js:427:7)
at startup (bootstrap_node.js:151:9)
I don’t understand why I’m getting the syntax error when I run the code. Could someone please help? Thanks.
I’m strugging on my project also. I typed it into an open js scrimba and am getting an error there as well though I haven’t the faintest clue why. it goes as follows:
console.log(‘Please enter rock, paper, or scissors.’);
};
console.log(getUserChoice(‘paper’));
I get a syntax error that tells me the else statement is somehow enexpected. any help or insight would be lovely. I reported it to the site as a bug but once i tested it somewhere else and got the same thing i was bewildered. Most importantly it’s because under the ‘get unstuck’ tab i reviewed the youtube video linked and that code is letter for letter what the instruction video guy typed up. Thanks in advance