简体   繁体   中英

My prompts don't seem to be working

I can't seem to get my prompts to show up in this code for an assignment I have to do for my class. It's supposed to show the amount of award points you have at the end, but none of the prompts show up once I add additional prompts and alerts to my JS. Any advice?

var numCoffees, awardPoints;

    numCoffees = prompt("How many coffees have you purchased?");
    if (numCoffees == 0)
      {awardPoints = 0;}

    if (numCoffees == 1)
      {awardPoints = 2;}

    if (numCoffees == 2)
      {awardPoints = 5;}

    if (numCoffees == 3)
      {awardPoints = 9;}

    if (numCoffees > 3)
      {awardPoints = ((9+2)*(numCoffees-3));}

  /*Determine Preferred Customer status*/

 var PreferredCustomer; 

    PreferredCustomer = prompt("Please say "yes" or "no" to indicate if you are a preferred customer.");
    if (PreferredCustomer == "yes")
      {awardPoints = awardPoints*2;}

  /*Display award points*/

    alert("awardPoints" + award points);

You have problem with your quotes. Change the first and last quotes to single quotes:

PreferredCustomer = prompt('Please say "yes" or "no" to indicate if you are a preferred customer.');

Alternatively, you can escape the inner quotes:

PreferredCustomer = prompt("Please say \"yes\" or \"no\" to indicate if you are a preferred customer.");

And, thanks to kakamg0's comment, fix the last line:

alert("awardPoints" + awardPoints);

Looks like there are some syntax issues here mainly. Logically the code is written. The way it is written is a bit faulty

As other have pointed out: The quoting is wrong

  • Having double quotes inside a string formed with double quotes will end the string statement at the first double quote paired.

  • award point in the alert box is not a valid variable. It contains spaces and it is not defined

Overall tidy up the code a bit and you'd end up with:

var numCoffees, awardPoints;

numCoffees = prompt("How many coffees have you purchased?");
if (numCoffees == 0) { awardPoints = 0; } else
if (numCoffees == 1) { awardPoints = 2; } else
if (numCoffees == 2) { awardPoints = 5; } else
if (numCoffees == 3) { awardPoints = 9; } else
if (numCoffees > 3) { awardPoints = ((9 + 2) * (numCoffees - 3));}

/*Determine Preferred Customer status*/

var PreferredCustomer;

PreferredCustomer = prompt("Please say 'yes'or 'no' to indicate if you are a preferred customer.");
if (PreferredCustomer == "yes") {
  awardPoints = awardPoints * 2;
}

/*Display award points*/

alert("awardPoints" + awardPoints);

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