Rock, Paper, Scissors

Hi all!
This is my first question on forums so please be gentle :?
I am a new coder working on the Rock , Paper or Scissors exercise. I am a little confused with how I can create two functions - getUserChoice and getComputerChoice then later in the if and if/else statements, I am able to refer to them as - userChoice and computerChoice ??? This goes against the way I think computers work.
I wouldn’t think that the computer would just know that one means the other without me first telling it so.
Insight please!!!
Thank you to all ; )

Computers are dumb, and only work with what we give them. When we write,

userChoice = getUserChoice(arg)

we are assigning the return value of the function to a variable. They are not the same thing. One is a function, the other is its output given some argument. The function in this case returns the lowercase representation of the user’s input.

Hi mtf,
I appreciate your reply!
I don’t see that part in the lesson. I have looked it over a couple times because I though maybe there was something like that but I had missed it.
I’ll take a look at the instructional video.
Thank you :slight_smile:
treedreamin

I would hold off on the video and try to work with the narration and written instructions. Carry out one task, and if needs be, add in a console.log() to test that stage of your work.

  • get user input and assign to a variable
  • pass that variable to a function that changes it to lower case and validates it against the three permitted inputs
  • return the output if valid else log an error message and return false
  • if the return is valid get computer choice
    • pass the two values to determine winner function
    • log the outcome
1 Like

This is the code I have written, and the outcome. I’m feeling a little intimidated posting this for all to see, but obviously I am missing something.
I am going through the instructions again. I’ll take your advice on the process…wish me luck!

Code writen:

const getUserChoice = (userInput) => {

userInput = userInput.toLowerCase();

if (userInput === ‘rock’ || userInput === ‘paper’ || userInput === ‘scissors’) {

return userInput;

} else {

console.log(‘Invalid input’)

}

};

console.log(getUserChoice(‘paper’))

function getComputerChoice() {

let randomNumber = Math.floor(Math.random() * 3);

switch (randomNumber) {

case 0:

return 'Rock';

break;

case 1:

return 'Paper';

break;

case 2:

'Scissors';

break;

}

};

console.log(getComputerChoice());

function determineWinner(userChoice, computerChoice) {

if (getUserChoice === getComputerChoice) {

return ‘The game is a tie!’ ;

}

if (getUserChoice === ‘rock’){

if (getComputerChoice === ‘paper’)

return ‘The computer won!’;

} else {

return ‘You won!’ ;

}

if (getUserChoice === ‘paper’){

if (getComputerChoice === ‘scissors’)

return 'The Computer won! ';

else {

 return 'You won!';

}

}

};

if (getUserChoice === ‘scissors’) {

if (getComputerChoice === ‘paper’)

return ‘You won!’;

else {

return 'Computer won!';

}

}

console.log(determineWinner(‘paper’, ‘paper’));

// prints ‘The game is a tie!’

console.log(determineWinner(‘paper’, ‘Scissors’));

// prints “The computer won!”

console.log(determineWinner(‘paper’, ‘rock’));

// prints ‘You won!’

console.log(determineWinner(‘rock’, ‘rock’));

// prints ‘The game is a tie!’

console.log(determineWinner(‘rock’, ‘paper’));

// prints ‘The computer won!’

console.log(determineWinner(‘rock’, ‘scissors’));

//prints ‘You won!’

console.log(determineWinner(‘scissors’, ‘scissors’));

// prints ‘The game is a tie!’

console.log(determineWinner(‘Scissors’, ‘paper’));

// prints “You won!”

console.log(determineWinner(‘scissors’, ‘rock’));

// prints “The computer won!”

Outcome:

paper
Paper
You won!
You won!
You won!
You won!
You won!
You won!
You won!
You won!
You won!

Hello! When you call a function, you need to be sure to put the parentheses () at the end of it:

function someFun(){
  return "FUNCTION";
}

console.log(someFun);//logs [Function: someFun]
console.log(someFun());//logs "FUNCTION"

You can also save the result of functions to variables:

function someFun(){
  return "This is the return value: a string";
}

let someVar = someFun();
console.log(someVar);//logs "This is the return value: a string"

If you declare a function outside any other function, you can use it in other functions:

function someFun(){
  return "This is the return value: a string";
}

function anotherFun(){
  let someVar = someFun();
  return someVar;
}
console.log(anotherFun());//logs "This is the return value: a string"

I hope this has given you some hints as to how to approach this task (from the code I’ve seen, you’ve almost got it; good job!)

Recall that the valid inputs for userChoice are rock paper scissors once transformed in the getUserChoice function. To match the computer choice it cannot have any upper case.

1 Like