I tried to make a silly little game in JavaScript where you're prompted to guess a number between 1-10. The computer then randomly picks a number and if it's the same as you guessed it says "You guessed right!", else "Try again!".
However, the computer input and the user input is NEVER the same! What am I doing wrong?
var userChoice = prompt("Make a guess from 1 to 10");
var computerChoice = Math.random();
if (computerChoice < 0.1) {
computerChoice = 1;
}
else if (computerChoice < 0.2){
computerChoice = 2;
}
else if (computerChoice < 0.3){
computerChoice = 3;
}
else if (computerChoice < 0.4){
computerChoice = 4;
}
else if (computerChoice < 0.5){
computerChoice = 5;
}
else if (computerChoice < 0.6){
computerChoice = 6;
}
else if (computerChoice < 0.7){
computerChoice = 7;
}
else if (computerChoice < 0.8){
computerChoice = 8;
}
else if (computerChoice < 0.9){
computerChoice = 9;
}
else {
computerChoice = 10;
}
var compare = function(choice1, choice2){
if (choice1 === choice2){
console.log("You guessed right!");
}
else{
console.log("Try again!");
}
};
compare(userChoice, computerChoice);