Problem in javascript with system of measurements in table

0

I am starting to learn a bit of javascript to do a small project and I have run into a problem that I have not been able to solve in any way, I hope you can help me.

  • I tell you a little about the project to know what it is: In a Increaseable table calls for measures [long] [width] [high] and In addition to this, the [weight] and the [quantity] are requested. After about calculations are obtained [Total Weight] [Total Volume]
  • At the end you must have 2 different [Total] results.
  • The result that will be housed in: [TotalAirUSD] must be calculated from the following way - > compare the [Total Weight] and [Total Volume] for know which is the GREATER between the two and save its value in a variable (var), then: if the variable is GREATER than 8 (var> 8) it must multiply by the variable rate (var * rate) to give as result [TotalAirUSD] - Otherwise [TotalAirUSD] is assigned a value of 20.
  • Here's the problem: if you enter one of these numbers "4 5 6 7 8" in the field [Weight] then [TotalAirUSD] takes a value of 20 automatically (with the rest of the numbers it works fine)

Complete project: link

Simplified project to the problem: link

  • Notes: When I enter a value in " Weight Total (totalweight) " multiplies by the value of " Air Rate " and returns the result in " Total Air USD " Except when entering any of these numbers: 4 , 5, 6, 7, 8, 9 does not meet the "IF > 8" condition and automatically goes to the total value of "20"
asked by Irving Ngr 13.07.2017 в 03:51
source

1 answer

0

It is a little difficult to read the code since it has several errors, in the part of the html it marks in red the errors, for example, in the following line you open a tag but you never close it

<th width="10%"><center>Length</th>

On line 123 of the html you are closing a form that never opened

As for the javascript in lines 88,89 and 90, you are calling the html () method instead of val (), what html does is bring the html code of the child nodes and val gets the value value value

totalweightvariable = $('#totalweight').html();
airtotalvar = $('#airtotal').html();
air = $('#airrate').html();

In line 88, totalweightvariable brings undefined since there is no element with the id totalweight

I recommend you to set some console.log to check the values that arrive, but remember to delete them when everything is already good, for example:

totalweightvariable = $('#totalweight').html();
console.log(totalweightvariable ); //imprime undefined
airtotalvar = $('#airtotal').html();
console.log(airtotalvar ); //cadena vacía
air = $('#airrate').html();
console.log(air ); //cadena vacía

In the end it should be like this

totalweightvariable = $('#totalweight').val(); //Solo asegurate que exista un elemento con id=totalweight
airtotalvar = $('#airtotal').val();
air = $('#airrate').val();

EDITO With the simplified error it is easier to see the problem, what happens is that you are comparing strings, val () returns a string even if they are numbers and you have to pass them to float

totalwvariable =parseFloat(document.querySelector('#totalweight').value);
airtotalvar = parseFloat(document.querySelector('#airtotal').value);
air = parseFloat($('#airrate').val());

When comparing strings it goes character by character if you have "35" and you compare it against "4" first it compares the "3" against the "4" and how it fulfills that 4 is greater than 3 it entered the if

    
answered by 13.07.2017 в 20:29