JavaScript IF ELSE

70 pts.
Tags:
IF ELSE statement
JavaScript
Web development
I have managed to get the prompt to work, but when i type a number into prompt it displays all outputs on webpage, like nodiscount5%discount etc, could someone please sort my program so it just shows one output, then i can study my errors and learn from it. thankyou. <HTML> <HEAD> <TITLE>Volume Discount</TITLE> <SCRIPT LANGUAGE = "JavaScript"> // A program to tell a customer what volume discount they can expect. var numberOfGiftBoxes; numberOfGiftBoxes = window.prompt ('Please enter now many boxes you are purchasing.', ''); numberOfGiftBoxes = parseFloat(numberOfGiftBoxes); if (numberOfGiftBoxes <= 3) { document.write("no discount"); } if (numberOfGiftBoxes >= 4) ! (numberOfGiftBoxes <= 11) { document.write("5% discount"); } if (numberOfGiftBoxes >= 12) ! (numberOfGiftBoxes <= 29) { document.write("10% discount"); } if (numberOfGiftBoxes >=30) { document.write("20% discount"); } </SCRIPT> </HEAD> <BODY> </BODY> </HTML>

Answer Wiki

Thanks. We'll let you know when a new response is added.

Try this (modified):
<pre>
if (numberOfGiftBoxes <= 3)
{
document.write(”no discount”);
}

if (numberOfGiftBoxes >= 4 && numberOfGiftBoxes <= 11)
{
document.write(”5% discount”);
}

if (numberOfGiftBoxes >= 12 && numberOfGiftBoxes <= 29)
{
document.write(”10% discount”);
}

if (numberOfGiftBoxes >=30)
{
document.write(”20% discount”);
}</pre>

Discuss This Question: 10  Replies

 
There was an error processing your information. Please try again later.
Thanks. We'll let you know when a new response is added.
Send me notifications when members answer or reply to this question.

REGISTER or login:

Forgot Password?
By submitting you agree to receive email from TechTarget and its partners. If you reside outside of the United States, you consent to having your personal data transferred to and processed in the United States. Privacy
  • Gunter9
    thankyou for trying, but this one does not seem to work either
    70 pointsBadges:
    report
  • carlosdl
    What is the result ?
    69,240 pointsBadges:
    report
  • Gunter9
    When i put that in, no windows prompt box appears asking for how many boxes to purchase
    70 pointsBadges:
    report
  • carlosdl
    I didn't post the complete page code, just the part that I thought needed to be changed. The complete code should be like this: <HTML> <HEAD> <TITLE>Volume Discount</TITLE> <SCRIPT LANGUAGE = "JavaScript"> // A program to tell a customer what volume discount they can expect. var numberOfGiftBoxes; numberOfGiftBoxes = window.prompt ('Please enter now many boxes you are purchasing.', ''); numberOfGiftBoxes = parseFloat(numberOfGiftBoxes); if (numberOfGiftBoxes <= 3) { document.write("no discount"); } if (numberOfGiftBoxes >= 4) && (numberOfGiftBoxes <= 11) { document.write("5% discount"); } if (numberOfGiftBoxes >= 12) && (numberOfGiftBoxes <= 29) { document.write("10% discount"); } if (numberOfGiftBoxes >=30) { document.write("20% discount"); } </SCRIPT> </HEAD> <BODY> </BODY> </HTML>
    69,240 pointsBadges:
    report
  • Gunter9
    when i try this one the window propmt box does not appear, but thankyou for trying, it is much appreciated
    70 pointsBadges:
    report
  • carlosdl
    You're right. This should work: <HTML> <HEAD> <TITLE>Volume Discount</TITLE> <SCRIPT LANGUAGE = "JavaScript"> // A program to tell a customer what volume discount they can expect. var numberOfGiftBoxes; numberOfGiftBoxes = window.prompt ('Please enter now many boxes you are purchasing.', ''); numberOfGiftBoxes = parseFloat(numberOfGiftBoxes); if (numberOfGiftBoxes <= 3) { document.write("no discount"); } if (numberOfGiftBoxes >= 4 && numberOfGiftBoxes <= 11) { document.write("5% discount"); } if (numberOfGiftBoxes >= 12 && numberOfGiftBoxes <= 29) { document.write("10% discount"); } if (numberOfGiftBoxes >=30) { document.write("20% discount"); } </SCRIPT> </HEAD> <BODY> </BODY> </HTML>
    69,240 pointsBadges:
    report
  • Gunter9
    I am afraid this one does not prompt either. Now this one prompts and works for no discount and 5 % discount <HTML> <HEAD> <TITLE>Volume Discount</TITLE> <SCRIPT LANGUAGE = "JavaScript"> //A program to tell a customer what volume discount they can expect.\ var numberOfGiftBoxes; numberOfGiftBoxes = window.prompt ('Please enter how many boxes you are purchasing.', ''); numberOfGiftBoxes = parseFloat(numberOfGiftBoxes); if (numberOfGiftBoxes <= 3) { document.write("no discount"); } else if (numberOfGiftBoxes >= 4 ) { document.write("5% discount") } else if (numberOfGiftBoxes <= 11) { document.write("5% discount") } else if (numberOfGiftBoxes >= 12) { document.write("10% discount") } else if (numberOfGiftBoxes <= 30) { document.write("10% discount") } if (numberOfGiftBoxes >30) { document.write("20% discount") } </SCRIPT> </HEAD> <BODY> </BODY> </HTML> but if you type number in like 45 to prompt it displays 5%discount20%discount
    70 pointsBadges:
    report
  • carlosdl
    I tested the last code I posted, and it works for me Previously it did not work because it had errors in the IFs parenthesis. Your code is only working for 0% and 5% discount because of this part: if (numberOfGiftBoxes <= 3) { document.write(”no discount”); } else if (numberOfGiftBoxes >= 4 ) { document.write(”5% discount”) } Every quantity that is greater than 3 will fit into the >= 4 condition. If you want to do it that way you should start evaluating the highest numbers of gift boxes first for example: if (numberOfGiftBoxes >30) { document.write(”20% discount”) } else if (numberOfGiftBoxes >= 12) { document.write(”10% discount”) } ... etc
    69,240 pointsBadges:
    report
  • Gunter9
    This one works, same as yours with brackets, many thanx <HTML> <HEAD> <TITLE>Volume Discount</TITLE> <SCRIPT LANGUAGE = "JavaScript"> // A program to tell a customer what volume discount they can expect. var numberOfGiftBoxes; numberOfGiftBoxes = window.prompt ('Please enter how many boxes you are purchasing.', ''); numberOfGiftBoxes = parseFloat(numberOfGiftBoxes) if (numberOfGiftBoxes <= 3) { document.write("no discount") } else if ( (numberOfGiftBoxes >= 4) && (numberOfGiftBoxes <= 11)) { document.write("5% discount") } else if ( (numberOfGiftBoxes >= 12) && (numberOfGiftBoxes <= 29)) { document.write("10% discount") } else if (numberOfGiftBoxes >= 30) { document.write("20% discount") } </SCRIPT> </HEAD> <BODY> </BODY> </HTML>
    70 pointsBadges:
    report
  • Stormshoppers
    I like this idea, I have been working on modifying a code to suit my needs, but am having no luck, maybe you guys could help me with this, I need this code to do % calculations for multiple products, otherwise I need to post like 100 of this code in one page: function processOrder() { var quantity = parseInt(document.frmOrder.txtQuantity.value); var unitPrice, totalPrice; // The unit price depends on the quantity order // The higher the quantity, the lower the unit price if( quantity < 5 ) unitPrice = 22.80; else if( quantity < 10 ) unitPrice = 20.80; else if( quantity < 15 ) unitPrice = 18.80; else if( quantity < 20 ) unitPrice = 16.80; else if( quantity < 25 ) unitPrice = 14.80; else if( quantity < 30 ) unitPrice = 12.80; else if( quantity < 35 ) unitPrice = 10.80; else if( quantity < 40 ) unitPrice = 8.80; else unitPrice = 6.80; totalPrice = quantity * unitPrice; document.frmOrder.txtUnitPrice.value = unitPrice.toFixed(2); document.frmOrder.txtTotalOrder.value = totalPrice.toFixed(2); // Create Advanced Product string for InternetSecure Processor document.frmOrder.Products.value = ""+ unitPrice +"::"+ quantity +"::VERSION 2010::Discounted lisenses::"; } I want it to work in 10% intervals for bulk orders, for instance 1-4 no discount, 5-9 10%, 10-19 15%, 20+ 20% is there any way you could help me out?
    10 pointsBadges:
    report

Forgot Password

No problem! Submit your e-mail address below. We'll send you an e-mail containing your password.

Your password has been sent to:

To follow this tag...

There was an error processing your information. Please try again later.

REGISTER or login:

Forgot Password?
By submitting you agree to receive email from TechTarget and its partners. If you reside outside of the United States, you consent to having your personal data transferred to and processed in the United States. Privacy

Thanks! We'll email you when relevant content is added and updated.

Following