JSFiddle - React, Tailwind, and code Playground
by Cosette Girardot
JavaScript
var userChoice = prompt("Do you choose sword, magic or talking?");
var computerChoice = Math.random();
if (computerChoice < 0.34) {
computerChoice = "sword";
}
else if(computerChoice < 0.67) {
computerChoice = "magic";
}
else {
computerChoice = "talking";
}
window.alert("You chose: " + userChoice);
window.alert("Computer: " + computerChoice);
var compare = function (choice1, choice2) {
if (choice1 === choice2){
window.alert ("The result is a tie!");
}
else if(choice1 === "sword"){
if (choice2 === "talking") {
window.alert ("sword wins");
}
else {
window.alert ("magic wins");
}
}
else if (choice1 === "magic"){
if (choice2 === "sword"){
window.alert ("magic wins");
}
else {
window.alert ("talking wins");
}
}
else if (choice1 === "talking"){
if (choice2 === "magic"){
window.alert ("talking wins");
}
else {
window.alert( "sword wins");
}
}
else if ( choice1 !== "magic" || choice1 !== "talking" || choice1 !== "sword"){
if (choice2 === "magic") {
window.alert ("That is a creative answer... but please pick sword, magic, or talking - lower case!")
}
else if(choice2 === "sword") {
window.alert ( " That is a creative answer... but please pick sword, magic, or talking - lower case!")
}
else {
window.alert ( " That is a creative answer... but please pick sword, magic, or talking - lower case!")
}
}
};
compare (userChoice, computerChoice);