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 7524131
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T03:00:11+00:00 2026-05-30T03:00:11+00:00

I am having a bit of trouble with a few math calculation in javascript.

  • 0

I am having a bit of trouble with a few math calculation in javascript.
The goal of this calculation is to generate a value when the user clicks on a text field.

For example:
1 Kilogram costs 32 cents to ship to America and the user wants to find out what 10KG will cost him which is $3.20. For this I have the following piece of javascript code:

    function calculate(num) {
        var weight  = document.getElementById('weight'+num);
        var price   = document.getElementById('price'+num);
        if(num == undefined || num == '' || num.length <= 0 || isNaN(weight.value) || isNaN(price.value)) return false;

        if(num == 1) multiplyBy = 0.32;
        if(num == 2) multiplyBy = 0.14;
        if(num == 3) multiplyBy = 0.24;
        if(num == 4) multiplyBy = 0.53;

        var sum = parseInt(document.getElementById('weight'+num).value) * multiplyBy;
        if(isNaN(sum)) return false;
        price.value = sum;
    }

The above code works perfectly fine, however when I reverse the process (someone has $3.20 and wants to find out how much KG he/she can ship with that (which is 10KG) the script returns: 9.375KG

The following code is used for this calculation:

function reverse(num) {
        var weight  = document.getElementById('weight'+num);
        var price   = document.getElementById('price'+num);
        if(num == undefined || num == '' || num.length <= 0 || isNaN(weight.value) || isNaN(price.value)) return false;

        if(num == 1) divideBy = 0.32;
        if(num == 2) divideBy = 0.14;
        if(num == 3) divideBy = 0.24;
        if(num == 4) divideBy = 0.53;

        var sum = parseInt(document.getElementById('price'+num).value) / divideBy;
        if(isNaN(sum)) return false;
        weight.value = sum;
    }

I honestly don’t grasp why it is failing, It would be much appreciated if someone could help me out with this.

  • 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-05-30T03:00:13+00:00Added an answer on May 30, 2026 at 3:00 am
    var sum = parseInt(document.getElementById('price'+num).value) / divideBy;
    

    You are forcing price into an integer before dividing it. So if price is 3.20, you are actually dividing 3 / 0.32, which is 9.375.

    Don’t force it into an integer.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm having a bit of trouble trying to get this quicksort algorithm to work
Im having a bit of trouble with this regex. I have a line that
Having a bit of trouble with the syntax where we want to call a
Having a bit of trouble using the List.Find with a custom predicate i have
Im having a bit of trouble getting my JSON to be recognised by my
I'm having a bit of trouble with the UTL_MAIL package in Oracle 10g, and
I'm having a bit of trouble trying to get class variables to work in
I'm having a bit of trouble iterating and retrieving width() values. $(.MyClass).each( function ()
I'm having a bit of trouble with a C# program I'm writing and it'd
I'm having a bit of trouble with the mktime function. On my production server,

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.