Reputation: 53
I am very new to all of this and I recently started learning JavaScript. To test my learning I made this simple script, Rock, paper, and scissors. It is something very similar to Codecademy project. The problem I am having is with the output, which comes out as 'undefined' and I can't figure out, what's giving this output, can someone please help?
const getUserChoice = userInput => {
userInput = userInput.toLowerCase();
if (userInput === 'rock') {
return 'Rock'
} else if (userInput === 'paper') {
return 'Paper' }
else if (userInput === 'scissors') {
return 'Scissors'}
else if (userInput === 'bomb') {
return 'Bomb'
} else {
return 'Please input a valid choice!'
}
}
const getComputerChoice = () => {
const numbers = (Math.floor(Math.random() * 3))
switch (numbers) {
case 0 : return "Rock";
break;
case 1 : return "Paper";
break;
case 2 : return "Scissors";
break;
}
}
const determineWinner = (userChoice, computerChoice) => {
if (userChoice === computerChoice) {
return 'It\'s a tie!!';
}
if (userChoice === 'rock') {
if (computerChoice === 'paper') {
return 'The Computer has won the game!!';
} else {
return 'Congratulation You have won the game!!';
}
}
if (userChoice === 'scissors') {
if (computerChoice === 'rock') {
return ('The Computer has won the game!!');
} else {
return ('Congratulations You have won the game!!');
}
}
if (userChoice === 'scissors') {
if (computerChoice === 'paper') {
return 'Cogratulations You have Won the game!!';
} else {
return 'The Computer has won the game!!';
}
}
if (userChoice === 'bomb') {
return 'Congratulation you Won!!'
}
};
const playGame = () => {
var userChoice = getUserChoice('rock')
var computerChoice = getComputerChoice()
console.log('You picked: ' + userChoice);
console.log('The computer picked: ' +computerChoice)
console.log(determineWinner(userChoice, computerChoice));
}
playGame()
Upvotes: 1
Views: 109
Reputation: 776
Welcome to programming then! Comment above is true - when you post, do try to be really specific about exactly what problem you're seeing. Makes it much easier to help you.
With that said though, I think from looking at the code above I can see what you mean. It's often helpful when debugging to walk through how your code will run:
So from following those steps its actually quite easy to see why determineWinner is undefined when you log it out... it doesn't return anything. The point of your getUserChoice function appears to be standardizing the inputs. But those standardized inputs are not being used properly later. You could consider storing those possible values in an array and then returning the lowercase value from that function?
Hope this helps - good luck!
Upvotes: 0
Reputation: 3043
The values you chose are capitalized, so, for example, you want to check if userChoice === 'Rock'
instead of userChoice === 'rock'
Fixed:
const getUserChoice = userInput => {
userInput = userInput.toLowerCase();
if (userInput === 'rock') {
return 'Rock'
} else if (userInput === 'paper') {
return 'Paper' }
else if (userInput === 'scissors') {
return 'Scissors'}
else if (userInput === 'bomb') {
return 'Bomb'
} else {
return 'Please input a valid choice!'
}
}
const getComputerChoice = () => {
const numbers = (Math.floor(Math.random() * 3))
switch (numbers) {
case 0 : return "Rock";
break;
case 1 : return "Paper";
break;
case 2 : return "Scissors";
break;
}
}
const determineWinner = (userChoice, computerChoice) => {
if (userChoice === computerChoice) {
return 'It\'s a tie!!';
}
if (userChoice === 'Rock') {
if (computerChoice === 'Paper') {
return 'The Computer has won the game!!';
} else {
return 'Congratulation You have won the game!!';
}
}
if (userChoice === 'Scissors') {
if (computerChoice === 'Rock') {
return ('The Computer has won the game!!');
} else {
return ('Congratulations You have won the game!!');
}
}
if (userChoice === 'Scissors') {
if (computerChoice === 'Paper') {
return 'Cogratulations You have Won the game!!';
} else {
return 'The Computer has won the game!!';
}
}
if (userChoice === 'Bomb') {
return 'Congratulation you Won!!'
}
};
const playGame = () => {
var userChoice = getUserChoice('rock')
var computerChoice = getComputerChoice()
console.log('You picked: ' + userChoice);
console.log('The computer picked: ' +computerChoice)
console.log(determineWinner(userChoice, computerChoice));
}
playGame()
Upvotes: 0
Reputation: 23379
Your userChoice
and computerChoice
are both capitalized. You are checking them against lowercase strings. Also, you're checking for scissors twice and not checking for paper.
const getUserChoice = userInput => {
userInput = userInput.toLowerCase();
if (userInput === 'rock') {
return 'Rock'
} else if (userInput === 'paper') {
return 'Paper'
} else if (userInput === 'scissors') {
return 'Scissors'
} else if (userInput === 'bomb') {
return 'Bomb'
} else {
return 'Please input a valid choice!'
}
}
const getComputerChoice = () => {
const numbers = (Math.floor(Math.random() * 3))
switch (numbers) {
case 0:
return "Rock";
break;
case 1:
return "Paper";
break;
case 2:
return "Scissors";
break;
}
}
const determineWinner = (userChoice, computerChoice) => {
if (userChoice === computerChoice) {
return 'It\'s a tie!!';
}
if (userChoice === 'Rock') {
if (computerChoice === 'Paper') {
return 'The Computer has won the game!!';
} else {
return 'Congratulation You have won the game!!';
}
}
if (userChoice === 'Paper') {
if (computerChoice === 'Rock') {
return ('The Computer has won the game!!');
} else {
return ('Congratulations You have won the game!!');
}
}
if (userChoice === 'Scissors') {
if (computerChoice === 'Paper') {
return 'Cogratulations You have Won the game!!';
} else {
return 'The Computer has won the game!!';
}
}
if (userChoice === 'Bomb') {
return 'Congratulation you Won!!'
}
};
const playGame = () => {
var userChoice = getUserChoice('rock')
var computerChoice = getComputerChoice()
console.log('You picked: ' + userChoice);
console.log('The computer picked: ' + computerChoice)
console.log(determineWinner(userChoice, computerChoice));
}
playGame()
Upvotes: 2
Reputation: 63
You only check for Rock and Sciccors in your determine winner method
const getUserChoice = userInput => {
userInput = userInput.toLowerCase();
if (userInput === 'rock') {
return 'Rock'
} else if (userInput === 'paper') {
return 'Paper' }
else if (userInput === 'scissors') {
return 'Scissors'}
else if (userInput === 'bomb') {
return 'Bomb'
} else {
return 'Please input a valid choice!'
}
}
const getComputerChoice = () => {
const numbers = (Math.floor(Math.random() * 3))
switch (numbers) {
case 0 : return "Rock";
break;
case 1 : return "Paper";
break;
case 2 : return "Scissors";
break;
}
}
const determineWinner = (userChoice, computerChoice) => {
if (userChoice === computerChoice) {
return 'It\'s a tie!!';
}
if (userChoice === 'Rock') {
if (computerChoice === 'Paper') {
return 'The Computer has won the game!!';
} else {
return 'Congratulation You have won the game!!';
}
}
if (userChoice === 'Scissors') {
if (computerChoice === 'Rock') {
return ('The Computer has won the game!!');
} else {
return ('Congratulations You have won the game!!');
}
}
if (userChoice === 'Paper') {//You mean paper here
if (computerChoice === 'Rock') {
return 'Cogratulations You have Won the game!!';
} else {
return 'The Computer has won the game!!';
}
}
if (userChoice === 'bomb') {
return 'Congratulation you Won!!'
}
};
const playGame = () => {
var userChoice = getUserChoice('rock')
var computerChoice = getComputerChoice()
console.log('You picked: ' + userChoice);
console.log('The computer picked: ' +computerChoice)
console.log(determineWinner(userChoice, computerChoice));
}
playGame()
Upvotes: 0