# Keep getting NaN and can't work out why, any help appreciated;

#1

Hi Guys,

Brand new to coding but getting Not a Number error, I don’t really know enough to work out why.

Read a number of threads but just found myself a little more confused

Thanks!

``````var userInput = 'Rock';
userInput= userInput.toLowerCase();

function getComputerChoice(){
switch(Math.floor(Math.random()*3)){
case 0:
return 'rock';
break;
case 1:
return 'paper';
break;
case 2:
return 'scissors';
break;
}
}

function determineWinner(userChoice, computerChoice) {
if (userChoice === computerChoice) {
return 'The game is a tie!';
}
if (userChoice === 'rock') {
if (computerChoice === 'paper') {
return 'You lose';
} else {
return 'You Win';
}
}
if (userChoice === 'paper') {
if (computerChoice === 'scissors') {
return 'You lose';
} else {
return 'You Win';
}
}
if (userChoice === 'scissors') {
if (computerChoice === 'rock') {
return 'You lose';
} else {
return 'You Win';
}
}
}

function playGame( ){
var userChoice= userInput;
var computerChoice= getComputerChoice();
console.log('You threw: ', + userChoice);
console.log('The computer threw: ', + computerChoice);
console.log (determineWinner(userChoice, computerChoice));
}

playGame();
``````

#2

I put comma’s in the playGame function and this made it expect a number it seems?

Solved this part for now

Will try to put a prompt in

#3

What you do is you work your way backwards from where you have your NaN until you reach where you got it from. Prints to the rescue.

#4

Thank you!
I’ll remember this for the future

#5

So, i finished this and ran the code in JS Bin and it worked fine. Added the bomb and the user prompt

``````var userInput = prompt('Choose your weapon\n\nRock   Paper   Scissors');
userInput= userInput.toLowerCase();

function getComputerChoice(){
switch(Math.floor(Math.random()*3)){
case 0:
return 'rock';
break;
case 1:
return 'paper';
break;
case 2:
return 'scissors';
break;
}
}

function determineWinner(userChoice, computerChoice) {
if (userChoice=== 'bomb') {
return 'You Win, cheater';
}
if (userChoice === computerChoice) {
return 'The game is a tie!';
}
if (userChoice === 'rock') {
if (computerChoice === 'paper') {
return 'You lose';
} else {
return 'You Win';
}
}
if (userChoice === 'paper') {
if (computerChoice === 'scissors') {
return 'You lose';
} else {
return 'You Win';
}
}
if (userChoice === 'scissors') {
if (computerChoice === 'rock') {
return 'You lose';
} else {
return 'You Win';
}
}
}

function playGame( ){
var userChoice= userInput;
var computerChoice= getComputerChoice();
console.log('You threw: ' + userChoice);
console.log('The computer threw: ' + computerChoice);
console.log (determineWinner(userChoice, computerChoice));
}

playGame();
``````

#6

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