Rock, Paper, Scissors

The easiest way I can think to do it is actually the way that @jagking suggested, albeit in JS rather than Python. :slight_smile:

let t = 'paper';

function checkThrow(janken) {
  let valid = ['rock','paper','scissors'];
  if (valid.includes(janken)) {
    console.log("Valid throw!");
  } else {
    console.log("Sorry, try again!");
  }
};

checkThrow(t);

Array types inherit from the prototype, which includes the includes() method. We can use this to test for the presence of the provided input in a pre-defined array of acceptable values. :slight_smile:

If you wanted to do it a really hacky (and probably thoroughly ill-advised) way, you could strap together a switch statement like so:

let t = 'paper';

function validateThrow(janken) {
  switch(janken) {
    case 'rock':
    case 'paper':
    case 'scissors':
      console.log("Valid selection!");
      break;
    default:
      console.log("Invalid choice!");
  }
};

validateThrow(t);

I would not advise that you do it that way, though. Essentially what we’re doing with the switch is having JS match any of the allowed cases, and then “fall through” the switch running everything we come across until we either hit break or exit the switch… which is very much not good behaviour.

It is, however, a fun example. :slight_smile:

1 Like

Yeah that wasn’t my brightest answer / moment :stuck_out_tongue: . My apologies @thegreysmith

Happy coding!

1 Like

Hi guys,
I am having some trouble with my codes and I don’t understand where is the problem.

const getUserChoice = userInput => {
userInput = userInput.toLowerCase();
if (userInput === ‘rock’ || userInput === ‘paper’ || userInput === ‘scissors’){
return userInput;
}else {
return ‘Error!’
}
}

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

if (randomNumber === 0){
console.log(‘rock’);
} else if (randomNumber === 1){
console.log(‘paper’);
}else if (randomNumber === 2){
console.log(‘scissors’);
}
}

function determineWinner(userChoice, computerChoice){
if (userChoice === computerChoice){
return ‘It was a tie!’;
} else if (userChoice === ‘rock’){
if (computerChoice === ‘paper’){
return ‘The computer Wins’;
} else if (computerChoice === ‘scissors’){
return ‘The user Wins’;
}
}else if (userChoice === ‘paper’){
if (computerChoice === ‘scissors’){
return ‘Computer Wins’;
}else if (computerChoice === ‘rock’){
return ‘User Wins’;
}

}else if(userChoice === 'scissors'){
  if (computerChoice === 'rock'){
    return 'Computer Wins';
  }else if (computerChoice === 'paper'){
    return 'User Wins';
  }
}

}

const playGame = () => {
const userChoice = getUserChoice(‘rock’);
const computerChoice = getComputerChoice();
console.log('Your choice was ’ + userChoice);
console.log('The computer choice was ’ + computerChoice);

}

playGame();


When I run the game, shows this results:

scissors
Your choice was rock
The computer choice was undefined

Why is the computer choice undefined??

I home someone could help me.
Thanks a lot!

was it supposed to be something else? why? did you define it somewhere? (no, then it would be something else) where?

Hi Ionatan,

Have you done this exercise before?

const playGame = () => {
const userChoice = getUserChoice(‘rock’);
const computerChoice = getComputerChoice();
console.log('Your choice was ’ + userChoice);
console.log('The computer choice was ’ + computerChoice);

}

This code was supposed to give me the User Choice and the Computer Choice.
The User Choice was Rock, but the Computer Choice is ‘Undefined’.

Instead of Undefined, it was supposed to give me Rock, Paper or Scissors.

Do you know what is wrong about that?

it is something you wrote isn’t it?
so if it isn’t behaving like you want, you’d turn your attention to it
the undefined is coming from somewhere, so you’d continue looking there, follow it until you find the cause

const determineWinner = (userChoice, computerChoice) =>{
  if (userChoice === computerChoice){
    return 'This game is a tie';
  }
  if (userChoice === 'rock'){
    if (computerChoice === 'paper') {
      return 'sorry computer won!';
    } else {
      return 'you won!';
    }
  }
  if (userChoice === 'paper'){
    if (computerChoice === 'scissors'){
      return 'computer won!';
    } else {
      return 'you won!';
    }
  }
  if (userChoice === 'scissors'){
    if (computerChoice === 'rock'){
      return 'computer won!';
    }else {
      return 'you won!';
    }
  }
};

console.log(determineWinner('scissors, rock'));
console.log(determineWinner('rock, rock'));

Why is my console only logging undefined? please help!!!

Hello, @brendenh3207541349, and welcome to the forums.

Double check your argument(s) in your function calls. :wink:

If you do this:

console.log(x)

And that prints undefined… then where did you get x from? Follow it, keep following it until you reach the cause.

Thank you!!! its always something so obvious!!!

1 Like

I got stuck on task 3/14, please indicate where I am mistaking.

My code so far reads as follows:

const getUserChoice = userInput => {
}
userInput = 
userInput.toLowerCase();
 if (userInput === 'rock' || userInput === 'paper' || userInput === 'scissors'){
  return userInput;
} else {
  console.log('Error!');
}
console.log(getUserChoice('Paper'));
// should print 'paper'
console.log(getUserChoice('fork'));
// should print 'Error!' and 'undefined'

Terminal returns:

/home/ccuser/workspace/javascript_101_Unit_3/Unit_3/rockPaperScissors.js:5
 if (userInput === 'rock' || userInput === 'paper' || userInput === 'scissors'){
     ^

ReferenceError: userInput is not defined
    at Object.<anonymous> (/home/ccuser/workspace/javascript_101_Unit_3/Unit_3/rockPaperScissors.js:5:6)
    at Module._compile (module.js:571:32)
    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)
    at bootstrap_node.js:542:3

Am I hopeless?

Cheers coders!

1 Like

Doesn’t the error message spell that out?

If you do this:

console.log(x)

and get told x isn’t defined, then you’d want to either not use x:

console.log(5)

or define it before using it:

const x = 5
console.log(x)

How about writing a very small function that accepts a number, adds 1 to it, and returns the result? That way you can show for yourself that you’re able to use arguments passed to the function, that is the thing you’re struggling with here right? So a simpler version with less code involved would be easier to stay in control of.

Then you can make small modifications of this code, maybe you would instead send in a string, and return a lower case version of that string … and so on, until you have your complete function.

So you’d make small changes, run it, observe that the thing you just wrote had the desired effect, and keep going.
If you dump a bunch of text into your file and then can’t fix it … that’s not a great strategy. You’re going to make mistakes, so you’ll need a strategy where you’re able to identify them and deal with them.

And yeah you’re supposed to be in enough control of your code that if something breaks you should be able to argue for why it works and then check the things you argued for and that would make you run into the problem - by methodically checking the things that you rely on, so yeah, you very much can write a whole lot of code and fix things later but then you better do that knowing that you can fix everything involved whatever it is.

1 Like