Help a newbie move on please :)


#1

I have completed the task (code below) I am unsure of what to do next to make the game fully playable? Any tips greatly appreciated. Apologies if this sounds obvious but I am just beginning my coding journey. Thanks.

const getUserChoice = userInput => {
userInput = userInput.toLowerCase();
if (userInput === ‘rock’ || userInput === ‘paper’ || ‘scissors’) {
return userInput;
} else {
console.log(‘Sorry that is not a vaild choice!’);
}
};
function getComputerChoice() {
Math.floor(Math.random() * 3);
switch (getComputerChoice) {
case 0:
return ‘rock’;
case 1:
return ‘paper’;
case 2:
return ‘scissors’;
}
}
function determineWinner (userChoice, computerChoice) {
if (userChoice === computerChoice) {
return ‘Game is a tie!’;
}
if (userChoice === ‘rock’) {
if (computerChoice === ‘scissors’) {
return ‘You win!’;
} else {
return ‘The computer wins!’;
}
}
if (userChoice === ‘paper’) {
if (computerChoice === ‘scissors’) {
return ‘The computer wins!’;
} else {
return ‘You win!’;
}
}
if (userChoice === ‘scissors’) {
if (computerChoice === ‘rock’) {
return ‘The computer wins!’
} else {
return ‘You win!’;
}
}
}
const playGame = () => {
const userChoice =
getUserChoice(‘rock’);
const computerChoice =
getComputerChoice();
console.log('You threw: ’ + userChoice);
console.log(determineWinner(userChoice, computerChoice));
}

playGame();


#2

Hi @aquiladj,

You can help make the topic/post better next time while using </> to format your code, because the code gets distorted in this forum without using the proper markdown function. More can refer here

And also preferably share the link towards the lesson so that others can have a look and offer their help. Though I think it might be a PRO project, any link would be of a great help for those who can visit that link. :slight_smile:


Back to your question,

This part:

function getComputerChoice() {
  Math.floor(Math.random() * 3);   //<== this value is not saved or used anywhere
  switch (getComputerChoice) { //<== getComputerChoice has undefined as value
  case 0:
    return ‘rock’;
  case 1:
    return ‘paper’;
  case 2:
    return ‘scissors’;
  }
}

What you need is a number value as the switch condition, how do you achieve that? You can consider:

  • Replace those Math.floor(Math.random() * 3) code into the switch

  • OR save the Math.floor(Math.random() * 3) into a variable and use that variable inside the switch.

The way it is now, your switch will not work because it is dealing with a variable with an undefined value.


Then this part:

const playGame = () => {
  const userChoice =
  getUserChoice(‘rock’);
  const computerChoice =
  getComputerChoice();
  console.log('You threw: ' + userChoice);
  console.log(determineWinner(userChoice, computerChoice));
}

the assignment of variables is advisable to be coded in single line, why make it harder to read?
example: let A = "Testing";

I also think that you might miss out another console.log that should be printing the computerChoice, after the You threw:...


Another small note:
There is a spelling error in console.log('Sorry that is not a vaild choice!');

Hope that helps. :slight_smile:


#3

Thank you so much for your speedy response. It is very supportive.
I have now managed to get it to work (thanks to your tips) but the user always throws ‘rock’, any clues as to why i need to work out?

userInput = userInput.toLowerCase();
if (userInput === 'rock' || userInput === 'paper' || 'scissors') {
  return userInput;
} else {
 console.log('Sorry that is not a valid choice!');
}
};
function getComputerChoice() {
let randomNumber = (Math.floor(Math.random() * 3))
switch (randomNumber) {
  case 0:
    return 'rock';
  case 1:
    return 'paper';
  case 2:
    return 'scissors';
}
}
function determineWinner (userChoice, computerChoice) {
  if (userChoice === computerChoice) {
    return 'Game is a tie!';
  }
  if (userChoice === 'rock') {
    if (computerChoice === 'scissors') {
      return 'You win!';
    } else {
      return 'The computer wins!'; 
    }
  }
  if (userChoice === 'paper') {
   if (computerChoice === 'scissors') {
     return 'The computer wins!';
   } else {
     return 'You win!';
   }
  }
  if (userChoice === 'scissors') {
    if (computerChoice === 'rock') {
      return 'The computer wins!'
    } else {
      return 'You win!';
    }
  }
}
const playGame = () => {
  const userChoice = 
        getUserChoice('rock');
  const computerChoice =
        getComputerChoice();
  console.log('You threw: ' + userChoice);
  console.log('Computer threw: ' + computerChoice)
  console.log(determineWinner(userChoice, computerChoice));
}

#4

Hi @aquiladj,

You’re welcome. Glad that you figure it all out and thanks for using the correct markdown formatting for the code. It helps. Just a little suggestion though:

const playGame = () => {
  const userChoice = getUserChoice('rock'); //<== write it in a single line, that's the usual practice
  const computerChoice = getComputerChoice(); //<== write it in a single line, that's the usual practice
  console.log('You threw: ' + userChoice);
  console.log('Computer threw: ' + computerChoice)
  console.log(determineWinner(userChoice, computerChoice));
}

And I believe you missed out some code while pasting it into your post. Notably:

const getUserChoice = userInput => {  //<== The first line

....
....
....

playGame(); //<== The last line

Back to your question,

Because of this part in your code, you gave the string value of "rock" as argument passed into getUserChoice() function which the returned result is then saved into the variable userChoice

const playGame = () => {
  const userChoice = getUserChoice('rock'); //<== look here, that is why the user choice is always "rock"
  const computerChoice = getComputerChoice(); 
  console.log('You threw: ' + userChoice);
  console.log('Computer threw: ' + computerChoice)
  console.log(determineWinner(userChoice, computerChoice));
}

The code is written that way, therefore, user will always throw “rock”. It is expected to behave that way, nothing is wrong. If you run the code several times, you will notice the computer choice will be different and the winning results will be different too.


However, if you want different input, you can manually give getUserChoice() function another argument such as "paper" or "scissors" replacing "rock", that’s the easy way.

OR (in slightly difficult mode), you will need to alter the code of getUserChoice() function and add some additional code to prompt user, asking for input. That can be done with some tinkering and exploration.


Happy Coding! :slight_smile:


#5

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