Rock, paper, scissors

const getUserChoice = userInput => {
userInput = userInput.toLowerCase();
if (userInput === ‘rock’ || userInput === ‘paper’ || userInput === ‘scissors’){
return userInput;
} else {
console.log(‘Error please try again’);
}
}

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

console.log(getComputerChoice());
console.log(getComputerChoice());
console.log(getComputerChoice());

Here is my code.
Ive followed the video guide but still getting an error with const randomNumber = Math.floor(Math.random() * 3); this line is the problem but I can’t see why. Can anyone help me?

Hi,

Thanks for sharing your code!

Can you copy the exact error message? It’s usually very useful for troubleshooting.

And also, try pressing the </> button before pasting your code. Errors and issues can often be seen more clearly with proper formatting. Otherwise it’s hard for people to tell (or rather, it’s nice to not make them do extra work for the service).

Consider your function declaration. Does this work as a function yet? How would the program know it’s not an object? How would you pass a parameter in? for example: getClassId("Biology")

One distinguishing characteristic of functions in their parameters (), even when they’re empty, so if you’re declaring a function without them, it should raise alarm bells.

const getComputerChoice =  {
	const randomNumber = Math.floor(Math.random() * 3);
    }
// will return error, it's not quite a function
// and it's not quite an object

Object declaration for comparison:

let getComputerChoice = {
	randomNumber : Math.floor(Math.random() * 3)
    }

console.log(getComputerChoice.randomNumber)
//outputs random number between 0-2
console.log(getComputerChoice)
// { randomNumber: 1 }

Function declaration with const:

const say_hi = function() {
    return "hi";
    }

console.log(say_hi());
//hi
console.log(say_hi);
//[Function: say_hi]
const say_something = function(something) {
    return something;
    }

console.log(say_something('hi'));
//hi
console.log(say_something);
//[Function: say_something]

One last thing of note, notice the difference in output when we take the parameters away from the declared objects and functions. It is useful to know, because objects are also very powerful tools. So it’s good to be able to verify that you’ve created the tool you want to create… even if at the moment it creates a similar output, it’s potential use will be inherently different.

console.log(getComputerChoice) // from my object declaration example
// { randomNumber: 1 }

console.log(say_hi);
//[Function: say_hi]

console.log(say_something);
//[Function: say_something]
1 Like

This is the error message

/home/ccuser/workspace/javascript_101_Unit_3/Unit_3/rockPaperScissors.js:11
const randomNumber = Math.floor(Math.random()*3);
^^^^^^^^^^^^
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)

Yes, the comment I wrote above should help you fix that.

I mention that specific line. Let me know if you have questions, though!

1 Like

Hi thanks for your help but now I have an issue with my switch statement

const getComputerChoice = {
randomNumber : Math.floor(Math.random() * 3)

switch (randomNumber) {
case 0:
return ‘rock’;
case 1:
return ‘paper’;
case 2:
return ‘scissors’;
break;
}
};

this is the error code am getting

/home/ccuser/workspace/javascript_101_Unit_3/Unit_3/rockPaperScissors.js:13
switch (randomNumber) {
^^^^^^
SyntaxError: Unexpected token switch
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)

Am really struggling with this module as the tips and help video are no help at all as the code don’t work.

thanks for your help

1 Like

hi there sorry I’ve finally sorted lol its working fine now.

2 Likes