My code works; but what is the more succinct way?


#1



https://www.codecademy.com/en/courses/javascript-beginner-en-Bthev-mskY8/1/4?curriculum_id=506324b3a7dffd00020bf661

The code below works; but it seems long-winded. What would be a more succinct way of communicating this game?


var userChoice = prompt("Do you choose rock, paper or scissors?");
var computerChoice = Math.random();
if (computerChoice < 0.34) {
	computerChoice = "rock";
} else if(computerChoice <= 0.67) {
	computerChoice = "paper";
} else {
	computerChoice = "scissors";
} console.log("Computer: " + computerChoice);

var compare = function(userChoice, computerChoice) {
if (userChoice === computerChoice) {
    return "The result is a tie";
}
else if (userChoice === "rock") {

    if (computerChoice === "scissors")
        return "rock wins";
    }   
    else {
        return "paper wins";
    }
}

var compare = function(userChoice, computerChoice) {
if (userChoice === computerChoice) {
    return "The result is a tie";
}
else if (userChoice === "paper") {
    if (computerChoice === "rock")
        return "paper wins";
    }
    else {
        return "scissors wins";
    }
}

var compare = function(userChoice, computerChoice) {
if (userChoice === computerChoice) {
    return "The result is a tie";
}
else if (userChoice === "scissors") {
    if (computerChoice === "paper")
        return "Scissors wins";
    }
    else {
        return "Rock wins";
    }
}

var compare = function(userChoice, computerChoice) {
if (userChoice === computerChoice) {
    return "The result is a tie";
}
else if (userChoice === "scissors") {
    if (computerChoice === "rock")
        return "Rock wins";
    }
    else {
        return "Scissors wins";
    }
};


#2
var userChoice =prompt("what do you want choose rock,paper,scissors");
console.log("your choice is"+" "+ userChoice)
var computerChoice=Math.random()
if(computerChoice<0.34){
computerChoice="rock"
}
else if(computerChoice<0.67){
computerChoice="paper"
}
else{
computerChoice="scissors"
};
console.log("computer's choice is"+" "+computerChoice);
if (userChoice==="rock" && computerChoice==="paper"){
console.log("paper wins")
}
else if(userChoice==="rock"&& computerChoice==="scissors"){
    console.log("rock wins")
}
else if(userChoice==="paper" && computerChoice==="scissors"){
    console.log("scissors wins")
    }
    else if(userChoice==="scissors" && computerChoice==="rock"){
        console.log("rock wins")
        }
        else if(userChoice==="scissors" && computerChoice==="paper"){
    console.log("scissors wins")
    }
        else if(userChoice==="paper" && computerChoice==="rock"){
        console.log("paper wins")
        }
    else
    {
      console.log ("amazing!!! the match is try!play again.")
        }

i just used &&(and) operator.if u have any question ask me.


#3

Hi , this part

var compare = function(userChoice, computerChoice) {
if (userChoice === computerChoice) {
    return "The result is a tie";
}

remove it before All your else if statement


#4

Later on in the course, you will learn about switches, which look like this:

switch (fruit) {
  case "Oranges":
    console.log("Oranges are $0.59 a pound.");
    break;
  case "Apples":
    console.log("Apples are $0.32 a pound.");
    break;
  case "Bananas":
    console.log("Bananas are $0.48 a pound.");
    break;
  case "Cherries":
    console.log("Cherries are $3.00 a pound.");
    break;
  case "Mangoes":
  case "Papayas":
    console.log("Mangoes and papayas are $2.79 a pound.");
    break;
  default:
    console.log("Sorry, we are out of " + fruit + ".");
}

console.log("Is there anything else you'd like?");

and you will make a second "Code your own adventure!" game using switches. You can use your knowledge of switches to create a shorter version of your RPS game.