Need help please, can't make return or variables


#1

Hello there,

I need your advice for this project, i can’t make return so i tried to make the Winner variable, but it doesn’t work too, do you know what I can do ? Thanks for your help…

const getUserChoice = userInput => {
  userInput = userInput.toLowerCase();
  if (userInput === 'rock' || 'scissors' || 'paper')
    return userInput
  else
    console.log('Error, not a valid choice')
};
const randomNumber = Math.floor(Math.random() * 3);
const getComputerChoice = () => {
  if (randomNumber === 0)
    return 'rock';
  else if (randomNumber === 1)
    return 'scissors';
  else if (randomNumber === 2)
    return 'paper';
};

const determineWinner = (userChoice, computerChoice) => {
  	if (computerChoice === userChoice) {
    	return'Tie';
	} else {
			switch(userChoice) {
				case 'rock':
    			computerChoice === 'paper' ?  Winner = 'Computer won !':  Winner = 'You won !'
   			break;
  			case 'paper':
  				computerChoice === 'scissors' ?  Winner = 'Computer won !':  Winner = 'You won !'
   			break;
  			case 'scissors':
  				computerChoice === 'rock' ?  Winner = 'Computer won !':  Winner = 'You won !'
  			break;
  			default:
      }
  }
}

const playGame = () => {
  const userChoice = getUserChoice('rock');
  const computerChoice = getComputerChoice();
  console.log('You chose: ' + userChoice);
  console.log('Computer chose: ' + computerChoice);
  console.log(Winner);
};

playGame()


#2

please post your code with format:

How do I format code in my posts?

and without all the ** and !, so we can run the code


#4

you are using the ternary operator wrong, you just need to do:

let Winner = condition ? string1 : string2

and then the comparison and ternary operator

string1 would be assigned to winner if condition is true, else string2 gets assigned to the variable

there are some good examples here:

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Conditional_Operator


#5

I’m not sure, because i just want to return the Computer won! or the You won ! in determineWinner, but i tried return but it did not work.


#6

Here is what i tried.

const determineWinner = (userChoice, computerChoice) => {
  	if (computerChoice === userChoice) {
    	return'Tie';
	} else {
			switch(userChoice) {
				case 'rock':
    			computerChoice === 'paper' ? return 'Computer won !': return 'You won !'
   			break;
  			case 'paper':
  				computerChoice === 'scissors' ? return 'Computer won !': return 'You won !'
   			break;
  			case 'scissors':
  				computerChoice === 'rock' ? return 'Computer won !': return 'You won !'
  			break;
  			default:
      }
  }
}

And here is the error

/home/ccuser/workspace/javascript_101_Unit_3/Unit_3/rockPaperScissors.js:24
computerChoice === ‘paper’ ? return ‘Computer won !’: return ‘You won !’
under return ----> ^^^^^
SyntaxError: Unexpected token return
at createScript (vm.js:53:10)
at Object.runInThisContext (vm.js:95:10)
at Module._compile (module.js:543:28)
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)


#7

here the same thing applies:

return condition ? value1 : value2

you only use a single return or variable assignment.


#8

Do you mean like this ? It’s not giving me the value :confused:

const determineWinner = (userChoice, computerChoice) => {
  	if (computerChoice === userChoice) {
    	return'Tie';
	} else {
			switch(userChoice) {
				case 'rock':
    			return computerChoice === 'paper' ? 'Computer won !':'You won !'
   			break;
  			case 'paper':
  				return computerChoice === 'scissors' ? 'Computer won !':'You won !'
   			break;
  			case 'scissors':
  				return computerChoice === 'rock' ? 'Computer won !':'You won !'
  			break;
  			default:
      }
  }
}

const playGame = () => {
  const userChoice = getUserChoice('rock');
  const computerChoice = getComputerChoice();
  console.log('You chose: ' + userChoice);
  console.log('Computer chose: ' + computerChoice);
  console.log(determineWinner);
};

#9

do you call the playGame function? the determineWinner function doens’t get properly called. This:

console.log(determineWinner);

is not the proper way to call the determineWinner function


#10

I think this is better:

const playGame = () => {
  const userChoice = getUserChoice('scissors');
  const computerChoice = getComputerChoice();
  console.log(`You threw: ${userChoice}`);
  console.log(`The computer threw: ${computerChoice}`);
  console.log(determineWinner(userChoice, computerChoice));
};

playGame();

Thanks 4 ur help ! :smiley:


#11

that certainly looks a lot better :slight_smile:


#13

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