dakben
dakben

Reputation: 59

What am I doing wrong here? I get the prompt but no alert comes up after I make a selection

I'm trying to build a rock, paper, scissors game in JS. I think I have all the logic done correctly, it must be a syntax error, but for the life of me I can't find it. Thanks.

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";
}
  var compare = function(choice1, choice2) {
      if(choice1 === choice2) {
          alert( "It's a tie.");
      }
    if (choice1 === "scissors") {
        if (choice2 === "rock") {
            alert("You lose")
    }
      if (choice2 === "paper") {
          alert("you win")
     }

 }
    if (choice1 === "paper") {
        if (choice2 === "rock") {
            alert("You Win.")
        }
        if (choice2 === "scissors") {
            alert("You lose.")
        }
  }
    if (choice1 ==="scissors") {
        if (choice2 === "rock"){
            alert("You lose.")
        }
        if (choice2 === "paper") {
            alert("You win.")
        }
    }
  }
  compare(userChoice,computerChoice);

Upvotes: 0

Views: 67

Answers (5)

Yin
Yin

Reputation: 1

I guess it may be a copy paste issue. The last if clause(line 31-38) should be for rock combination instead of scissors.

Suggested Corrected code:

if (choice1 ==="rock") {
    if (choice2 === "scissors"){
        alert("You win.")
    }
    if (choice2 === "paper") {
        alert("You lose.")
    }
}

Your code:

if (choice1 ==="scissors") {
    if (choice2 === "rock"){
        alert("You lose.")
    }
    if (choice2 === "paper") {
        alert("You win.")
    }
}

Hope this helps.

Upvotes: 0

Mister Jojo
Mister Jojo

Reputation: 22320

My way...

const choices = ['rock','paper','scissors']

playBt.onclick=()=>
  {
  if (playBt.textContent==='play!')
    {
    let h = parseInt( humanChoice.value )
      , c = Math.floor(Math.random() *3)
      , win = (h+1) %3
      ;
    result.textContent = ' Computer choise is : ' + choices[c] 
                        + ((c===h) ? " -> It's a tie." 
                          : (c===win) ? " -> You lose." : " -> You win.")
    playBt.textContent = 'clear'
    }
  else
    {
    result.textContent = '?'
    playBt.textContent = 'play!'
    }
  }
<select id="humanChoice">
    <option value="0" selected >rock</option>
    <option value="1">paper</option>
    <option value="2">scissors</option>
  </select>
  
  <button id="playBt">play!</button>

  <p id="result">?</p>

Upvotes: 0

balexandre
balexandre

Reputation: 75093

First of all Welcome to StackOverflow, now your question ...

to tell you plain-and-simple you kind'a forgot to test for rock ... look at your code, you do the scissors, the paper, then scissors again 🤔

and for future programing, don't hold a float and a string in the same variable, javascript is forgiven, but it's still bad coding habits 😊👌

it would be more evident if you refractored your code a bit :

var userChoice = prompt("Do you choose rock, paper or scissors?");

var rnd = Math.random(); // use a new variable to hold the float number
var computerChoice = rnd < 0.34 
      ? "rock" : rnd <= 0.67 
          ? "paper" : "scissors";

// just so the example does not use alert() function
var alert = function(msg) { console.log(msg); }

var compare = function(choice1, choice2) {
  if (choice1 === choice2) alert( "It's a tie.");

  if (choice1 === "scissors") {
    if (choice2 === "rock") alert("You lose");
    if (choice2 === "paper") alert("you win");
  }

  if (choice1 === "paper") {
    if (choice2 === "rock") alert("You Win.");
    if (choice2 === "scissors") alert("You lose.");
  }

  if (choice1 ==="rock") { // you have "scissors" again here
    if (choice2 === "scissors") alert("You lose.")
    if (choice2 === "paper") alert("You win.");
  }
}

compare(userChoice,computerChoice);

Upvotes: 1

DCR
DCR

Reputation: 15685

this is not meant to be an answer but as I look at your compare function it appears to me it can be greatly simplified. Consider the following:

let choice1 = user choice: 1,2 or 3 (rock,paper,scissor)
let choice2 = computer choice: 1,2 or 3 (roc, paper,scissor)

choice1 = choice1 % 3;

if (choice1 == choice2){
   tie;
}else if(choice1 > choice2){
   choice1;
}else
  choice2;
}

Upvotes: 0

Tom
Tom

Reputation: 765

The issue come from the last if block, bellow a snippet with the correction :

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";
}
var compare = function(choice1, choice2) {
  if (choice1 === choice2) {
    alert("It's a tie.");
  }
  if (choice1 === "scissors") {
    if (choice2 === "rock") {
      alert("You lose")
    }
    if (choice2 === "paper") {
      alert("you win")
    }

  }
  if (choice1 === "paper") {
    if (choice2 === "rock") {
      alert("You Win.")
    }
    if (choice2 === "scissors") {
      alert("You lose.")
    }
  }
  if (choice1 === "rock") {
    if (choice2 === "paper") {
      alert("You lose.")
    }
    if (choice2 === "scissors") {
      alert("You win.")
    }
  }
}
compare(userChoice, computerChoice);

Upvotes: 0

Related Questions