Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

164
Views
What is the statement i need next to determine my output given a total?

I'm quite new to JavaScript so apologies in advanced, but I'm wanting too learn!

In my html form I am asking the customer for 3 numbers (number 1,2,3) and then in my JavaScript I am calculating the total of all of them. I need to work out my next bit of code so I can:

  1. Given the total of the numbers give I can print out to the customer you're item is free (less than 180) or it has a cost (more than 180)
  2. Given the number is under or over a certain amount return an error message

Where would be the best place to go with this ?

function Calculate() {
    var number1 = document.getElementById("number1").value;
    var number2 = document.getElementById("number2").value;
    var number3 = document.getElementById("number3").value;
    // var totalOfNumbers = Number(number1) + Number(number2) + Number(number3);
    document.getElementById("total").innerHTML = Number(number1) + Number(number2) + Number(number3);
 }
about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

You need to use an if construct.

The code would be :-

function Calculate() {
    var number1 = document.getElementById("number1").value;
    var number2 = document.getElementById("number2").value;
    var number3 = document.getElementById("number3").value;

    // use parseInt instead of Number()
    var totalOfNumbers =
        parseInt(number1) + parseInt(number2) + parseInt(number3);

    // string message to be displayed
    var message = "";

    // enter you own values
    var minAmount = 0;
    var maxAmount = 1000;

    // checking if total of the numbers is under or over a certain amount;
    // if it is, then showing an error message.
    if (totalOfNumbers < minAmount || totalOfNumbers > maxAmount) {
        message = "Error! Please enter valid amounts."
    } 
    // Checking if total of the numbers is under 180;
    // if it is, then displaying the item is free.
    else if (totalOfNumbers < 180) {
        message = "You're item is free!";
    } 
    // Checking if total of the numbers is greater than or equal to 180;
    // if it is, then displaying the item is not free, and its cost.
    else {
        message = "You're item is not free. It has a cost of $" + totalOfNumbers;
    }

    document.getElementById("total").innerHTML = message;
}
about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!