简体   繁体   中英

Javascript is only allowing one if statement, and no else if/else

I'm trying to use an if, else if, else statement, but it doesn't seem to work once I add the else if or else. If I just use the if statement, everything works just fine, but once I add the else if or else, it stops working.

var portAnswer = "";
var strQuestion = "Did you make it to port on time?";
var strDefault = "Yes/No";
portAnswer = prompt(strQuestion, strDefault);

var strMessage;

$(document).ready(function() {
  if (portAnswer == "yes") {
    strMessage = "Arghh, you are awarded extra doubloons!";
  } else if (portAnswer == "no") {
    strMessage = "Arggh, walk that there plank!");
  } else {
    strMessage = "Arggh, I don't understand!");
  }

  var strOutput = document.getElementById("promptParagraph");
  strOutput.textContent = strMessage;
});

Remove the extra end parentheses that are showing up as errors in your JavaScript console:

  if (portAnswer == "yes") {
    strMessage = "Arghh, you are awarded extra doubloons!";
  } else if (portAnswer == "no") {
    strMessage = "Arggh, walk that there plank!";
  } else {
    strMessage = "Arggh, I don't understand!";
  }

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