Rock, Paper, or Scissors project

Hi everyone,

I would really appreciate if someone could clarify couple of points from Rock, Paper, or Scissors project:

  1. I did the project walkthrough and Carlos did not use “break” while entering the “switch” code, I thought without it the console would run all the possible options and will show several of them but it did not?

  2. Why when building the determineNumber function the parameters used were ‘userChoice’ & ‘computerChoice’ not ‘getUsercChoice’ & ‘getComputerChoice’ or ‘get’ is not counted/ readable ? (if it is not readable then why do we have to use at all?)

I haven’t watched the video, but if he didn’t use break he probably used return at the end of each case. return sends control flow back to the line of code that called the function along with a value, so there is no need for break following a return statement. The break would never be reached.

getUserChoice and getComputerChoice are functions. userChoice and computerChoice are variables assigned to the values returned by the functions.

Consider the following example, and notice the different outputs depending on whether a break or return is executed along with whether or not the function is called alone, or as the argument to a console.log() call. Also notice the difference between returning a value as opposed to not explicitly returning anything.

const switchExample = someInt => {
  switch(someInt) {
    case 0:
      console.log("Hi.");
      break;
    case 1:
      console.log("Hello.");
      return;
    case 2:
      console.log("Bye.");
      return "Random Message";
  }
  console.log("A break was executed.");
}

//Consider the different ouputs here:
switchExample(0);
//output:
//Hi
//A break was executed.

console.log(switchExample(0));
//output:
//Hi
//A break was executed.
//undefined

switchExample(1);
//output:
//Hello.

console.log(switchExample(1));
//output:
//Hello.
//undefined

switchExample(2);
//output:
//Bye.

console.log(switchExample(2));
//output:
//Bye.
//Random Message
2 Likes

Im also struggling with the rock paper scissors project
const getComputerChoice=()=>{

let number;

number=Math.floor(Math.random()*3);

switch(number){

case 0:

console.log('rock');

break;

case 1:

console.log('paper');

break;

case 2:

console.log('scissors');

break;

}

}
outputs
paper
undefined

I dont understand where the undefined comes from.

Hello @nikuzealice930104gma, welcome to the forums! The reason you are getting undefined is because the getComputerChoice function does not return anything. When a function does not return anything, JS gives that function a default return value of undefined.

1 Like

But then I called the function and its return both undefined and the rock paper or scissor how do i get rid of the undefined. I tried to But I failed

What does the code you use to call the function with look like? If you have it inside a console.log(), like this: console.log(getComputerChoice()), then simply get rid of the console.log(), as this won’t log the returned value of the function-which is undefined at the moment.

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

const computerChoice = getComputerChoice();
console.log('The computer move is ', computerChoice);

The computer move is undefined

So the soluttion is a rubbish.

So how to get rid of this undefined? All was done according to Learn Javascript: Rock, Paper, Scissors - YouTube

Please don’t post in the same thing in two different places.

1 Like