[Solved] The prompt value returns undefined in Javascript [closed]


The code is correct and working properly, you just forgot the important part of showing it.

You aren’t writing the result to the actual document. You call the function and it returns properly, but you aren’t showing the answer visibly. Try this code:

var userChoice = prompt("What would you like to play?"),
    computerChoice = Math.random();
if (computerChoice <= 0.34){
    computerChoice = "Rock";
}
else if (computerChoice >= 0.35 && computerChoice <= 0.67){
		computerChoice = "Paper";
}
else {
	computerChoice = "Scissors";
}	
document.write("Computer's choice is " + computerChoice);
var compare = function(choice1, choice2) {
	if (choice1 === choice2){
		return "This result is a tie!";
	}
	else if (choice1 === "Rock") {
		if (choice2 === "Scissors"){
			return "Rock Wins!";
		}
		else {
			return "Paper Wins!";
		}
	}
	else if (choice1 === "Paper"){
		if (choice2 === "Rock"){
			return "Paper Wins";
		}
		else {
			return "Scissors wins";
		}	
	}
	else if (choice1 === "Scissors"){
		if (choice2 === "Rock"){
			return "Rock WINS";
		}
	    else {
			return "Scissors wins";
		}
	} 
    else {
        return "Invalid input! Type either rock, paper, or scissors!"
    }
};

document.write(", " + compare(userChoice, computerChoice));

You were missing the last document.write to write the result to the document. You can format it yourself. I’d also suggest adding another case if the input isn’t valid. You could also put the main prompt in a function and call recursion if the input isn’t valid.

5

solved The prompt value returns undefined in Javascript [closed]