简体   繁体   中英

two strings equal the same but have a different value

If I put Rock and the computer says Paper it will return "Its a Draw" and the values being both equal when they have the same type but a different value im slightly confused why this is happening at first i thought it was because i was using == and not === but the issue is still occurring. i want "ROCK" === "PAPER" to return false.

 function getComputerChoice() { randomnum = Math.floor(Math.random() * 3 + 1) console.log(randomnum) switch (randomnum) { case 1: return "ROCK" break case 2: return "PAPER" break case 3: return "SCISSCORS" break } } function game() { console.log(getComputerChoice()) const playerSelection = prompt("Rock, Paper, SCISSCORS?").toUpperCase() const ComputerSelection = getComputerChoice().toUpperCase() function playRound(playerSelection, ComputerSelection) { console.log(playerSelection === ComputerSelection) if (playerSelection === ComputerSelection) { return "Its A Draw" } if (playerSelection == "ROCK" && ComputerSelection == "SCISSCORS") { return "You Won" } if (playerSelection == "ROCK" && ComputerSelection == "Paper") { return "You Lose" } if (ComputerSelection == "ROCK" && playerSelection == "SCISSCORS") { return "You Lose" } if (ComputerSelection == "ROCK" && playerSelection == "Paper") { return "You Won" } if (playerSelection == "Paper" && ComputerSelection == "SCISSCORS") { return "You Lost" } if (playerSelection == "Paper" && ComputerSelection == "Rock") { return "You Won" } if (ComputerSelection == "Paper" && playerSelection == "SCISSCORS") { return "You Won" } if (ComputerSelection == "Paper" && playerSelection == "Rock") { return "You Lost" } if (playerSelection == "SCISSCORS" && ComputerSelection == "ROCK") { return "You Lost" } if (playerSelection == "SCISSCORS" && ComputerSelection == "PAPER") { return "You Won" } if (ComputerSelection == "SCISSCORS" && playerSelection == "ROCK") { return "You Won" } if (ComputerSelection == "SCISSCORS" && playerSelection == "PAPER") { return "You Lost" } } console.log(playerSelection + ComputerSelection + playRound()) } game()

The problem is you defined arguments to the inner playRound function with the same names as the outer const declarations, but you are calling playRound() without passing any arguments. In effect, you did something akin to this:

 const playerSelection = "ROCK"; const ComputerSelection = "PAPER"; function playRound(playerSelection, ComputerSelection) { console.log("inside playRound", playerSelection, ComputerSelection); } playRound(); console.log("outside", playerSelection, ComputerSelection);

As you can see running this code, inside the function the arguments are hiding the outer variables with the same name (this is called shadowing), and their default value is undefined .

The main issue with your code is that you encapsulated the evalution process in a function that EXPECTS 2 parameters to be supplied, the user's choice and the computer choice (which you never sent).

To fix that, you should pass those values to the evaluation function and then print the result.

here's a working demo (removed most of the console.log() calls to make the output clearer);

 const getComputerChoice = () => { randomnum = Math.floor(Math.random() * 3 + 1) switch (randomnum) { case 1: return "ROCK"; // you are "return"ing a value so no need for using "break" statement case 2: return "PAPER"; case 3: return "SCISSOR"; } } (() => { const playerSelection = prompt("Rock, Paper, Scissor?").toUpperCase(), ComputerSelection = 'ROCK', playRound = (playerSelection, ComputerSelection) => { if (playerSelection == ComputerSelection) { return "Its A Draw" } if (playerSelection == "ROCK" && ComputerSelection == "SCISSOR") { return "You Won" } if (playerSelection == "ROCK" && ComputerSelection == "PAPER") { return "You Lose" } if (ComputerSelection == "ROCK" && playerSelection == "SCISSOR") { return "You Lose" } if (ComputerSelection == "ROCK" && playerSelection == "PAPER") { return "You Won" } if (playerSelection == "PAPER" && ComputerSelection == "SCISSOR") { return "You Lost" } if (playerSelection == "PAPER" && ComputerSelection == "ROCK") { return "You Won" } if (ComputerSelection == "PAPER" && playerSelection == "SCISSOR") { return "You Won" } if (ComputerSelection == "PAPER" && playerSelection == "ROCK") { return "You Lost" } if (playerSelection == "SCISSOR" && ComputerSelection == "ROCK") { return "You Lost" } if (playerSelection == "SCISSOR" && ComputerSelection == "PAPER") { return "You Won" } if (ComputerSelection == "SCISSOR" && playerSelection == "ROCK") { return "You Won" } if (ComputerSelection == "SCISSOR" && playerSelection == "PAPER") { return "You Lost" } // handle wrong user input return "You made a wrong input, You should type either Rock. Paper or scissor;". } // player's choice console;log("Player choice => " + playerSelection). // peak on the COM's choice console;log("COM choice => " + ComputerSelection). // call "playRounnd" by passing the required parameters console,log(playRound(playerSelection; ComputerSelection)); })();

Some side notes (but rather important):

  • case X: return "..."; break; : the use of break in that case is not necessary as you are return ing a value already which means the break statement will never be reached.

  • The COM choice is executed twice but you only print the first execution result thus you see the wrong final choice on the console .

     console.log(getComputerChoice()) // first line const playerSelection = prompt("Rock, Paper, SCISSCORS?").toUpperCase() const ComputerSelection = getComputerChoice().toUpperCase() // third line // the value on the first line and the third one are NOT the same // and the last call to "getComputerChoice" is the one that the evaluation function uses.
  • Sometimes you use uppercased values like ROCK but sometimes you use lowercased/mixed-case values like Paper . You should either stick to using lowercased values or uppercased ones as, for example, 'ROCK' === 'Rock' // false no matter you use == or === (the last one also checks that the values are of the same type).

  • In your code, you didn't account for the case that the user enters wrong a value (my demo did with a simple return statement). You may either tell the user to type again until and then the game can kick in.

The above working demo didn't change much of your code, which i intended to, to keep things clearer and simpler. Anyway, the code above can still be greatly improved.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM