Question

I am making a rock paper scissors game, and I want alerts to determine who picked what and who won, but the alerts dont pop up. I found multiple errors going back and fixed them, double checked everything, and even gave a triple check, but the alerts still do not pop up?

This is one of the three functions (they all are similar):

function rock() {
  var computerChoice = Math.random();
  if (computerChoice < 0.34) {
    computerChoice = "rock";
  } else if (computerChoice < 0.67) {
    computerChoice = "paper";
  } else {
    computerChoice = "scissors";
  }
}
if (computerChoice === "rock") {
   alert("Link and Computer both chose Rock! It's a Tie!");
} else if (computerChoice === "scissors") {
   alert("Link chose Rock and Computer chose Scissors! Computer took a heart of damage!");
} else {
   alert("Link chose Rock and Computer chose Paper! Link took a heart of damage!");
}
}
Was it helpful?

Solution

I think the problem is on the line indicated:

function rock() {
  var computerChoice = Math.random();
  if (computerChoice < 0.34) {
    computerChoice = "rock";
  } else if (computerChoice < 0.67) {
    computerChoice = "paper";
  } else {
    computerChoice = "scissors";
  }
} // <-- I don't think you want this closing brace here
if (computerChoice === "rock") {
   alert("Link and Computer both chose Rock! It's a Tie!");
} else if (computerChoice === "scissors") {
   alert("Link chose Rock and Computer chose Scissors! Computer took a heart of damage!");
} else {
   alert("Link chose Rock and Computer chose Paper! Link took a heart of damage!");
}
}

The closing brace I've indicated brings an end to the rock() function. As it stands, the rock() function doesn't actually do anything; it assigns a value "rock", "paper" or "scissors" at random to a local variable, but the function ends without the variable being used, so the value is lost.

The rest of the code is then run once when the JavaScript is first loaded into the browser. At this point, computerChoice will be undefined, and hence you will get a JavaScript error.

If we remove the errant brace, and reformat the code, we get the following:

function rock() {
  var computerChoice = Math.random();
  if (computerChoice < 0.34) {
    computerChoice = "rock";
  } else if (computerChoice < 0.67) {
    computerChoice = "paper";
  } else {
    computerChoice = "scissors";
  }

  if (computerChoice === "rock") {
     alert("Link and Computer both chose Rock! It's a Tie!");
  } else if (computerChoice === "scissors") {
     alert("Link chose Rock and Computer chose Scissors! Computer took a heart of damage!");
  } else {
     alert("Link chose Rock and Computer chose Paper! Link took a heart of damage!");
  }
}

I ran this function several times. Each time it alerted one of the three messages at random.

OTHER TIPS

You can't return an alert. Alert is a method call. Remove the return in front of the alert, and the alert will open:

if(computerChoice === "rock"){
    alert("Link and Computer both chose Rock! It's a Tie!");
}

You don't need the return. So, change this:

if(computerChoice === "rock"){
  return alert("Link and Computer both chose Rock! It's a Tie!");
}

to this:

if(computerChoice === "rock"){
  alert("Link and Computer both chose Rock! It's a Tie!");
}

This works and uses less detours to get there :)

function rock() {
  var replies = new Array(
    'Link and Computer both chose Rock! It\'s a Tie!',
    'Link chose Rock and Computer chose Scissors! Computer took a heart of damage!',
    'Link chose Rock and Computer chose Paper! Link took a heart of damage!');
  alert(replies[Math.floor(replies.length * Math.random())]);
}

Clarification: Math.random() returns a float in the range [0..1>
replies.length * Math.random() returns a float in the range [0..3>
Math.floor(replies.length * Math.random()) returns 0, 1 or 2.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top