Can someone tell me what is wrong in my code, please?


#1

var userChoice = prompt("Do you choose rock, paper, scissors or rope?");
var computerChoice = Math.random();
if (computerChoice <= 0.25) {
computerChoice = "rock";
} else if(computerChoice === 0.26 - 0.50) {
computerChoice = "paper";
} else if(computerChoice === 0.51 - 0.75){
computerChoice = "scissors";
} else {
computerChoice = "rope"
} console.log("Computer: " + computerChoice);
var compare = function(choice1, choice2 ){
if(choice1 === choice2) {
return "The result is a tie!";
}

else if(choice1 === "rock") {

if(choice2 === "scissors") {
    return "Rock wins";
}
else if(choice2 === "paper"
    return "Paper wins";
}
else if(choice2 === "rope"
    return "Rope wins";
}
else {
    return "The input is not valid. Please use the correct word in the correct case."
    }

}
else if(choice1 === "paper") {

if(choice2 === "scissors") {
    return "scissors wins";
}
else if(choice2 === "rock"
    return "Paper wins";
}
else if(choice2 === "rope"
    return "Rope wins";
}
else {
    return "The input is not valid. Please use the correct word in the correct case."
    }

}
else if(choice1 === "scissors") {

if(choice2 === "paper") {
    return "Scissors wins";
}
else if(choice2 === "rock"
    return "Rock wins";
}
else if(choice2 === "rope"
    return "Scissors wins";
}
else {
    return "The input is not valid. Please use the correct word in the correct case."
    }

}
else if(choice1 === "rope") {

if(choice2 === "scissors") {
    return "Scissors wins";
}
else if(choice2 === "paper"
    return "Rope wins";
}
else if(choice2 === "rock"
    return "Rope wins";
}
else {
    return "The input is not valid. Please use the correct word in the correct case."
    }

};
compare(userChoice, computerChoice)


#2

You must NOT tamper with the functionality of the compare function
which is
given a userChoice and a computerChoice a string Value will be returned
which you can capture in a variable
like

var theResult = compare(userChoice,computerChoice);
console.log( theResult );

If you want to control the userChoice validity, you will have to create
a function like getUserChoice and within do a validation...

==================================

1 To extend the userChoice-prompting with a validity-check, you would
embed the prompting into a function-body...
So you would start by
var getUserChoice = function() { your prompting here };

2 If you are extending the choice-possibillity, you have to extend the
so-called truth-table.

Your new Decisions-Table: fill-in ?? wins...

              | ch-2  |  ch-2  | ch-2     | ch-2   |
              | rock  |  paper | scissors | rope   |
  ------------|-------|--------|----------|--------|
              |       | paper  |  rock    | ????   |
 choice1  rock|   X   | wins   |  wins    |  wins  | (1st else if )
              |       |        |          |        |
  ------------|-------|--------|----------|--------|
              | paper |        |scissors  |  ? ?   |
 choice1 paper| wins  |    X   |  wins    |  wins  | (2nd else if )
              |       |        |          |        |
  ------------|-------|--------|----------|--------|
              |  rock |scissors|          |  ?  ?  | 
 ch-1 scissors|  wins | wins   |   X      |  wins  | (3rd else if )
              |       |        |          |        |
  ------------|-------|--------|----------|--------|
              |  ? ?  | ? ? ?  |  ? ? ?   |        | 
 ch-1 rope    |  wins | wins   |   wins   |    X   | (4th else if )
              |       |        |          |        |
  ------------|-------|--------|----------|--------|

3 What you want is a loop-construct...
create the getUserChoice function using return-statement
create the getComputerChoice function using return-statement
creat the compare function using return-statement

As you are using return-statements you call the compare-function
var theResult = compare(userChoice,computerChoice);
and thus having the =result= of the game in the theResult variable

===========================================

var playTheGame =function() {
    var playAgain =true;
    var userChoice="";
    var computerChoice="";
    var theResult="";

   while (playAgain) {

       //execute the pre-declared getUserChoice-function
       var userChoice = getUserChoice();
       console.log("User choice is " + userChoice);

        //execute the pre-declared getComputerChoice-function
        var computerChoice = getComputerChoice();
        console.log("Computer choice is " + computerChoice);

        //execute the compare-function and capture the-result
        theResult = compare(userChoice,computerChoice);
        console.log("The result is " + theResult);

    if (theResult === "It is a tie!") {
       playAgain = true;
    } else {
      playAgain = false;
    }

    //end of -while- loop is reached, and if VALUE of playAgain === true
    // the loop will start over
    } //closing the while-loop
 }; //closing the function-body of playTheGame

// call the function playTheGame
playTheGame();