Rock, paper and scissors

In the Rock, Paper and Scissor project in the Function.

const getUserChoice = userInput =>{

userInput = userInput.toLowerCase();

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

return userInput;

} else{

console.log(`Invalid Input`)

}

}

const getComputerChoice = () =>{

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

switch(randomNumber) {

case 0:

  return 'rock';

  break;

case 1:

  return 'paper';

  break;

case 2:

  return 'Scissors';

  break;

}

}

const determinesWinner = (userChoice , computerChoice) =>{

if(userChoice === computerChoice){

return 'The game was a TIE!';

} else if(userChoice === ‘rock’){

  if(computerChoice === 'paper'){

    return 'Computer Won!'

  } else {

    return 'User Won!'

  }

} else if(userChoice === ‘paper’){

  if(computerChoice === 'rock'){

    return 'User Won!';

  } else {

    return 'Computer Won!';

  } 

} else if(userChoice === ‘scissors’){

  if(computerChoice ==='rock'){

    return 'Computer Won!';

  } else {

    return 'User Won!';

  }

}

}

[quote=“786karanbadhwar, post:1, topic:553813, full:true”]
In the Rock, Paper and Scissor project in the Function.

const getUserChoice = userInput =>{

userInput = userInput.toLowerCase();

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

return userInput;

} else{

console.log(`Invalid Input`)

}

}

const getComputerChoice = () =>{

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

switch(randomNumber) {

case 0:

  return 'rock';

  break;

case 1:

  return 'paper';

  break;

case 2:

  return 'Scissors';

  break;

}

}

const determinesWinner = (userChoice , computerChoice) =>{

if(userChoice === computerChoice){

return 'The game was a TIE!';

} else if(userChoice === ‘rock’){

  if(computerChoice === 'paper'){

    return 'Computer Won!'

  } else {

    return 'User Won!'

  }

} else if(userChoice === ‘paper’){

  if(computerChoice === 'rock'){

    return 'User Won!';

  } else {

    return 'Computer Won!';

  } 

} else if(userChoice === ‘scissors’){

  if(computerChoice ==='rock'){

    return 'Computer Won!';

  } else {

    return 'User Won!';

  }

}

}

It is working good if i do this way
/*function playGame(){

const userChoice = getUserChoice(‘rock’);

console.log(User threw: ${userChoice});

const computerChoice = getComputerChoice();

console.log(Computer threw: ${computerChoice});

console.log(determinesWinner(userChoice, computerChoice));

}

playGame();*/
but why it is not working if i do this way
console.log(determinesWinner(console.log(getUserChoice(‘rock’)), console.log(getComputerChoice())));

1 Like

Why do you have console.logs nested in one-another? console.log doesn’t return anything-it simply prints to the console. Also, here:

Since console.log returns nothing, you are actually passing undefined into the determinesWinner function.

3 Likes

The reason behind putting console.log in the determinesWinner function to verify are they right result because i was getting results but very weird and it was printing the results driven from the other functions so that i can match them.

And one more thing , so i am calling a function inside a console.log but why it will not return anything anyhow it was a print command and plus passed as an argument.
And sorry i forgot to thank you for helping me through this trouble.

console.log is not designed to return anything-it’s job is to print stuff to the console. It’s like if you made a function:

function print(){
  console.log("This prints!");
}

You wouldn’t expect to get a return value from this function, would you? (In fact, all functions return something-if there is no return implicitly or explicitly stated, then a function returns undefined.)


The reason it doesn’t work this way

Is as I said, console.log technically returns undefined. So, you are actually calling determinesWinner with undefined and undefined, as the two console.logs return the undefined. Since there isn’t and avenue in your logic for undefined, yet both parameters are, you should always get a tie when you run this line.

okay thankyou so much i got the point. i really appreciate your helps.
Thank You

1 Like

This topic was automatically closed 41 days after the last reply. New replies are no longer allowed.