Code in Javascript, using if, else and prompt

I am trying to write a code to answer the following. I have options of prizes. To be eligible for the prizes, you need to be within the age range of 20 and 40. If you are not eligible an alert saying you are not eligible will appear. If you are eligible; prompt will ask you for which prize you want and you need to answer with a numeric value. After they answer they will receive an alert that says “You will receive (prize, collected from the array) in your post within 2 weeks.”

I have gotten this far with my code:

var prize = [
    "0 = iPhone",
    "1 = iPad",
    "2 = iMac",
    "3 = iPod"
];
var age = prompt("Please enter your age");
if (20 >= age && age <= 40){
    alert("Sorry, you are not eligible for a prize.");
} else {
    prompt("Which prize would you like to receive?");
} if prize = 0 {
    alert("You have chosen iPhone!");
}

You might want to update your if statement conditions. Currently the “Sorry” message occurs when you are the valid age range, which seems to be the opposite of what you require.

    var prize = [
    "0 = iPhone",
    "1 = iPad",
    "2 = iPod",
    "3 = iMac"
];
var age = prompt("How old are you?");
if (age <= 20 && 40 >= age) {
    alert("Sorry, you are not eligible for a prize.")
} else {
    prompt("Which prize would you like to receive?")
} if (prize = [0]){
    alert("Prize " + prize[0] + "has been shipped to you " + prize);
} else if (prize = [1]){
    alert("Prize " + prize[1] + "has been shipped to you " + prize);
}
} else if (prize = [2]){
    alert("Prize " + prize[2] + "has been shipped to you " + prize);
} else if (prize = [3]){
    alert("Prize " + prize[3] + "has been shipped to you " + prize);
} 

Thanks, Paul. However, that was not the issue. I need help figuring if the entire code is right. I have tweaked the code a little bit. In the last Prompt I would like the name of the prize to appear togheter with the sentense.

We’ll let’s start with the conditions that you stated earlier.

if (age >= 20 && age <= 40) {
    // get prize
}
if (...) {
    ...
} else {
    alert("Sorry, you are not eligible for a prize.");
}

It helps for people to know the number range when entering number. 0 is normally not used either, so we can either have nothing in the 0 spot of the array, or subtract 1 from what the person entered.

if (age >= 20 && age <= 40) {
    var num = prompt("Which prize would you like to receive? [1-4]");
}
...

You could also add code that checks if the entered value is a number, and checks if the number is within a valid range. But that’s not currently listed in the requirements.

if (age >= 20 && age <= 40) {
    var num = prompt("Which prize would you like to receive? [1-4]");
    alert("You have chosen " + prize[num] + " in your post within 2 weeks!");
}
...

I hope that breakdown of code updates in response to the requirements, is somewhat helpful.

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.