 # Code to do different mathematical calculation in each row?

I need to get [D] and [E] from another inputs and in each row I have a different mathematical (power of & divided by )
any one know code to get the result of each mathematical calculation ?
and How I get [D] and [E] from another function ???
this is the code of HTML:

``````<td>Daily aeroplane landings for all runways:<br>
Annual aeroplane landings all runways  ÷ 365 days per annum Daily aeroplane landings all runways
<div class="row gtr-uniform">
<div><input type="text" class="total" name="demo-r" id="number" value="" style="height: 2em" /></div>
<div  id="outpoput"></div><br/></div>
<br/>Average annual aeroplane mass for annual aeroplane landings for all runways:<br>
<span class="auto-style1">Annual aeroplane landing mass</span> ÷
<span class="auto-style1">Annual aeroplane landings</span>=
<span class="auto-style1">Average annual aeroplane mass for annual aeroplane landings for all runways </span></div>
<div class="row gtr-uniform">
<div><input type="text" name="demo-av" id="ber" value="" style="height: 2em; width: 60%" /></div>
<div><input type="text" name="demo-ev" id="rom" value="" style="height: 2em; width: 60%" /></div>
<div><input type="text" name="demo-nv" id="tot" value="" style="height: 2em" /></div>

``````

1 Like

You are going to have to take the first try at writing the JavaScript needed to solve this one.

1 Like

please help me cuze I’m new in programming and I keep trying for 3 days but no result at all

If you are new to programming, I suggest you start our Basic JavaScript course, which will help give you some kind of foundation to be able to work on this problem. Once you have some code you have tried, please reply back and then we can give you some pointers.

I have this code :

``````<html>
<table>
<tr>
<th>
Quanity
</th>
<th>
Unit Price
</th>
<th>
Total
</th>
</tr>
<tr class="txtMult">
<td>
<input name="txtEmmail" class="val1"  />
</td>
<td>
<input name="txtEmmail" class="val2"/>
</td>
<td>
<span class="multTotal">0.00</span>
</td>
</tr>
<tr class="txtMult">
<td>
<input name="txtEmmail" class="val1"  />
</td>
<td>
<input name="txtEmmail" class="val2"/>
</td>
<td>
<span class="multTotal">0.00</span>
</td>
</tr>
<tr class="txtMult">
<td>
<input name="txtEmmail" class="val1"  />
</td>
<td>
<input name="txtEmmail" class="val2"/>
</td>
<td>
<span class="multTotal">0.00</span>
</td>
</tr>
<tr class="txtMult">
<td>
<input name="txtEmmail" class="val1"  />
</td>
<td>
<input name="txtEmmail" class="val2"/>
</td>
<td>
<span class="multTotal">0.00</span>
</td>
</tr>
<tr class="txtMult">
<td>
<input name="txtEmmail" class="val1"  />
</td>
<td>
<input name="txtEmmail" class="val2"/>
</td>
<td>
<span class="multTotal">0.00</span>
</td>
</tr>
<tr>
<td colspan="3" align="right">
Grand Total# <span id="grandTotal">0.00</span><br>
input Total# <span id="grandTotal1">0.00</span>
</td>
</tr>
</table>
</html>
<script>
\$(".txtMult input").keyup(multInputs);
function multInputs() {
var mult = 0;
var count = 0;
var avg = 0;
\$("tr.txtMult").each(function () {
for (var i = 0; i > 0; i++)
count++;
var \$val1 = \$('.val1', this).val();
var \$val2 = \$('.val2', this).val();
var \$total = (\$val1 * 1) * (\$val2 * 1)
\$('.multTotal',this).text(\$total);
mult += \$total;
count +=\$val2;
});
\$("#grandTotal").text(mult);
\$("#grandTotal1").text(count);
}
});
</script>
``````

If I input for example:1 &2 &3
The output of (grandTotal1) is 123 but I want the output is the sum =6
how can I do it?

You’re actually close. If you change exactly three lines, it fixes a couple problems you’re having.

First two lines to be changed are

``````var \$val1 = \$('.val1', this).val();
var \$val2 = \$('.val2', this).val();
``````

Here, jQuery is returning the string value of each of these elements, which isn’t really what you want. You’re using a `* 1` hack to force them to numbers, but it would be easier to do this when you’re getting the values themselves:

``````var \$val1 = Number(\$('.val1', this).val());
var \$val2 = Number(\$('.val2', this).val());
``````

with that, `\$val1` and `\$val2` are coerced into numbers directly, with no hacks involved.

Doing that, the third line to be changed is the total:

``````var \$total = (\$val1 * 1) * (\$val2 * 1);
``````

simply becomes

``````var \$total = \$val1 * val2;
``````

That said, you will really benefit from following through on @RandellDawson’s suggestion, that you take the time and work through the basic Javascript course. It will give you some fundamentals that, it seems, are questionable at the moment.

EDIT: To see the code working, with only the changes I’d mentioned, take a look at this repl.

thank u for this but I have another q
the result in`<span id="grandTotal">0.00</span>` only How I can change it to be in `<input type="text"/>`???

Give that input an id or something you can use as a selector, maybe `grandTotalInput`, then you could simply do:

``````\$("#grandTotalInput").val(mult);
``````

`\$(...).val()` is an overloaded function - you can use it to both get the value of the form element, or if you specify a value, to set that form element’s value.

Also, I’ve updated the repl to reflect this and one other change – Currency should be rounded to two decimal places.

again, if my code doesn’t seem clear to you, work through the FCC Basics curriculum. It’s all there!

it doesn’t work `<input type="text" id="grandTotalInput"/>0.00<br>`

``````\$("#grandTotalInput").value(mult);
``````

I want the result to be in text input

yeah, should be `val` not `value` - I’m doing too many things at once.

could u help me in this code