Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8771807
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T17:52:59+00:00 2026-06-13T17:52:59+00:00

I am trying to create a script which automatically shows a price depending on

  • 0

I am trying to create a script which automatically shows a price depending on the entered value.

My price discounts could be:

1 item or more = "£5";
5 items or more = "£30";
10 items or more = "£55";

So when the user types “7” in an input box, the price is displayed as £30*7.

The only way I know how to do this is by making an if else statement for each case, but I am guessing there’s an easier way?

This is my pseudo code:

<script>

function calc() {
var amountVar = document.getElementById('amount').value;

var discount = new Array();
discount[1] = "£5";
discount[5] = "£30";
discount[10] = "£55";

match = discount where amountVar matches key or more;

document.getElementById('price').innerHTML = match;
}

</script>

<input onkeyup="calc();" id="amount">
<br>
Price: <p id="price"></p>
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-13T17:53:00+00:00Added an answer on June 13, 2026 at 5:53 pm

    Rather than an if/else, you can put them all in an array and just go through the array in a for loop until you find the matching discount. This has several advantages, the main one being that it’s trivial to edit the discounts array without writing new code.

    // array must be sorted by qty
    var discounts = [{qty:1, discount:5}, {qty:5, discount:30}, {qty:10, discount:55}];
    
    function calcPrice (qty) {
        qty = +qty;
    
        if (qty > 0)
        {
          // look through the array from the end and find first matching discount
          for (var i = discounts.length; i--;) {
              if (qty >= discounts[i].qty) {
                  return discounts[i].discount;
              }
          }
        }
    
        return 0;
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to create a script that will automatically select In Person Interview in
I'm trying to use this script (which extracts the domain from a URL): CREATE
I'm trying to create a batch script that will delete all sub directories which
I'm trying to write a simple PHP script which automatically sets up new etherpads
I am trying to create an expect script which will send a different password
I am trying to create a batch script which would perform the same action
I'm trying to create a script of which will log into drupal, and then
I'm trying to create a script that runs in the background automatically and cycles
Greetings. I am trying to create an autoconf configure script that automatically checks for
What I'm trying to do is create a PowerShell script which gets a list

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.