Let's say I have some numbers that I want to multiply and add.
var a = 5;
var b = 10;
var c = 2;
var d = 3;
I want to sum b and c then multiply that by a, then add d. Easy right?
If if were a normal equation the formula would look like: (a * (b + c) + d)
But how do I do that in JQuery?
(Note: the reason for JQuery is that I'll be getting these numbers from fields and divs... and placing a total elsewhere, etc.)
By default script language does not know type as int or float. So you can fix that by multiplying 1 to the value you expect to be a number.
var a = 5;
var b = 10;
var c = 2;
var d = 3;
var total = a*1 * (b*1 + c*1) + d*1;
convert your value into float or integer first before you do calculation. Example:
var a = parseFloat($(this).val());
var b = parseInt($("#b").attr("data"));
var c = (a+10)*b;
$("#result").text(c.toFixed(2));
Just store the values in variables before you apply them to the equation:
var a = +$("#input1")[0].value;
var b = +$("#input2")[0].value;
var c = +$("#input3")[0].value;
var d = +$("#input4")[0].value;
$("#output")[0].value = a*(b+c) + d;
The plus sign before the jquery function is there to force the string value into a Number value.
Correct JQuery is 100% javascript.
Although, worth mentioning just use parseint() for the values that you get from text field
You can still do the calculation using normal javascript, just refer to the contents using jQuery selectors if necessary.
Related
I'm doing some simple math in Javascript, but my equation's result is drastically different than what it should be. The math is:
3.05+(((0.32*0)+3.28)+(1+(0.19*0))*(2.66*1^2))*1;
When I did it out by hand, and then used Wolfram Alpha (https://www.wolframalpha.com/) I get the correct result of 8.99. However, when I use the equation in Javascript I mysteriously get 6.33
The actual equation looks like
VO2move = VO2rest+(((C1*g2)+VO2walkmin)+(1+(C2*g2))*(C3*s2^2))*t2;
but I removed all the variables in an attempt to debug (I thought it might be some error where I needed parseInt)
Here are the whole functions for reference
function calc(){
var temp = 0;
var total = 0;
for(i = 0; i<sArr.length; i++){
total = total + calc2(i);
}
var o = document.getElementById("output");
o.value = total;
}
function calc2(i){
var s = document.getElementById("s"+i);
var g = document.getElementById("g"+i);
var t = document.getElementById("t"+i);
var VO2walkmin = 3.28;
var VO2rest = 3.05;
var C1 = 0.32;
var C2 = 0.19;
var C3 = 2.66;
var Cdecline = 0.73;
var s2 = s.value;
var g2 = g.value;
var t2 = t.value;
var negGrade = g.value;
if(g2 < 0){g2 = 0};
//VO2move = ((C1 * g2)+VO2walkmin)+((1+(C2*g2))*(C3*s2^2)); //ORIGINAL TRANSCRIPTION
//VO2move = VO2rest+(((C1*g2)+VO2walkmin)+(1+(C2*g2))*(C3*s2^2))*t2; // TRANSLATED FROM COPY PASTE
VO2move = 3.05+(((0.32*0)+3.28)+(1+(0.19*0))*(2.66*1^2))*1; // COPY - PASTED FROM EXCEL
return VO2move;
}
Even naked numbers I still get the output of 6.33. I'm totally puzzled, and any help is appreciated.
You need to take the power (exponentiation) operator ** instead of the bitwise XOR operator ^.
console.log(3.05+(((0.32*0)+3.28)+(1+(0.19*0))*(2.66*1**2))*1);
I'm working on a project for a computers class, and am new to JavaScript. I keep getting NaN for output, and have looked for answers and haven't found any. (Project is a compound interest calculator and UI design is done.)
I'm not really sure what to try, cause I'm a JS newbie.
var i = getNumber("amountInput");
var c = getNumber("compoundedInput");
var l = getNumber("lengthInput");
var r = getNumber("rateInput");
var rc = r / c;
var cl = c * l;
onEvent("calculateButton", "click", function() {
var rca = 1 + rc;
var p = Math.pow(rca, cl);
var f = i * p;
setText("outputArea", f);
});
Output should be a number, but I am getting NaN.
You need to set all the variables inside the onEvent function, so that you get the values of the inputs after the user clicks the button. You're setting them when the page is first loaded, and the inputs will be empty at that time.
onEvent("calculateButton", "click", function() {
var i = getNumber("amountInput");
var c = getNumber("compoundedInput");
var l = getNumber("lengthInput");
var r = getNumber("rateInput");
var rc = r / c;
var cl = c * l;
var rca = 1 + rc;
var p = Math.pow(rca, cl);
var f = i * p;
setText("outputArea", f);
});
NaN in JS means 'not a number'. (mdn reference)
Usually, you would see NaN when you're trying to convert a non-numeric string to a number, either explicitly (e.g. parseFloat('3')), or implicitly (e.g. the expression 3 * 'a' will implicitly try to convert 'a' to a number). Worth noting, dividing by zero does not give NaN in JS (it gives infinity or -infinity); however, 0/0 also gives NaN.
For your use case, you're expecting getNumber to return a numeric, but it seems like it isn't. Look at your getNumber code (or copy it in your question).
if you want to get number from input in html you cant use getNumber()
<input type="text" id="number" >
<button id="submit">Submit</button>
and for geting the number:
const number = documnet.getElementById('number');
const submitBtn = document.getElementById('submit');
submitBtn.addEventListener('click', () => {
const plusOne = 1 + Number(number.value);
console.log(plusOne)
});
Hi I have two variables in JS Like:
var a = 223620.42
var b = 1200.1234
I am using Calculation Like:
var c = parseFloat(a) + parseFloat(b);
So the result should be = 224820.5434
But it returning 224820.54340000002
Please Suggest me what I am doing wrong here. Thanks in advance
Just Round-off the last Values. See the Code Below:
var a = 223620.42
var b = 1200.1234
var c = parseFloat(a) + parseFloat(b);
var numb = c.toFixed(4);
alert(numb);
hope it will solve you problem.
Try Using "toPrecision" method, and specify the desired length of the number.
var a = 223620.42
var b = 1200.1234
var c = parseFloat(a) + parseFloat(b);
c= c.toPrecision(3)
A simple question..
var x = document.getElementById('xNum');
var y = document.getElementById('xNum');
var result = x * y;
document.write(result);
and
<div id="xNum">20</div>
<div id="yNum">50</div>
It displays 20 and 50. why not calculating 20 * 50? Why does it get as a integer or how can I get numbers in an div?
Thanx!
I don't get any result with that:
var x = document.getElementById('xNum').innerHTML;
var y = document.getElementById('xNum').innerHTML;
var result = parseInt(x) * parseInt(y);
document.write(result);
Use parseInt and process it on their HTML,
var result = parseInt(x.innerHTML) * parseInt(y.innerHTML)
If you don't need to support browsers priot to IE9, you should use textContent instead of innerHTML.
If your numbers might be floats you should check out parseFloat instead
If you need to be able to handle numbers like 012 you should specify the radix parameter as they might be interpreted the wrong way by parseInt.
In this case you should use parseInt(x.innerHTML,10)
it should be
var x = document.getElementById('xNum').innerHTML;
var y = document.getElementById('yNum').innerHTML;
var result = x * y;
document.write(result);
Parse them into integers:
var x = document.getElementById('xNum');
var y = document.getElementById('yNum');
var result = parseInt(x.innerHTML, 10) * parseInt(y.innerHTML, 10);
The value you are getting is a string, so in order to use it as a number you should cast it to the integer (or float):
var x = +document.getElementById('xNum').innerHTML;
var y = +document.getElementById('xNum').innerHTML;
var result = x * y;
I used unary + operator, there are another methods like parseInt, Number constructor, etc.
By now the possible ways would have been exhausted, but here's an example with textContent:
var x = document.getElementById('xNum'),
y = document.getElementById('yNum'),
toIntNum = function(element) {
return parseInt(element.textContent || element.innerText || 0, 10);
},
result;
result = toIntNum(x) * toIntNum(y);
Demo
Js:
var x = document.getByElementId('xNum').innerHTML;
var y = document.getByElementId('xNum').innerHTML;
var result = parseInt(x) * parseInt(y);
document.write(result);
you must cast as int so calculation done. By default the value consider as string .
var x = document.getByElementId('xNum');
var y = document.getByElementId('xNum');
var result = parseInt(x) * parseInt(y); //use parseInt or parseDouble
document.write(result);
and
<div id="xNum">20</div>
<div id="yNum">50</div>
it give 1000
You have to use parseInt() function in javascript for parsing a string to return an integer.
Your code should be like this :
var x = document.getElementById('xNum');
var y = document.getElementById('yNum');
var result = parseInt(x.innerHTML) * parseInt(y.innerHTML);
document.write(result);
How would I add together the values pulled from two separate jQuery.html() calls? Example:
<div id="a">27</div>
<div id="b">3</div>
$(document).ready(function(){
var a = $("#a").html();
var b = $("#b").html();
var c = a + b;
});
All the above does is concatenate var a and b into c (i.e. c = 273) because a & b are strings. How do I get the actual values so that I can properly add them?
You can do either
var a = +$("#a").html();
or
var a = parseInt($("#a").html(), 10);
both of which cast the string to an integer. For more info, see How do I convert a string into an integer in JavaScript?.
Use the parseInt() function:
$(document).ready(function(){
var a = parseInt($("#a").html(), 10);
var b = parseInt($("#b").html(), 10);
var c = a + b;
});
You can use + in both operands
var c = +a + +b;
use text() instead of html().
var x = parseInt( $(el).text(), 10 );