javascript - parseInt on array and sum issue -
By using jQuery, I have an array population with checkbox values then I created the loop to loop through the array , Parses the checkbox values in the whole, assigns them the value for the sum and "total". When I run it, I do not get a number, I suspect something is up for the loop. Does anyone show me what I am doing wrong here and please forgive me if my coding is not as clean as I want, I am just starting in JS.
// Create array and select the checkbox values var allvals = []; $ ('Input: checkbox [name = brand]: check'). Each (function () {allVals.push ($ (this) .val ()); //allVals.map(number);}); For (var i = 0; i & lt; allvals.length; i ++) {var total allvals [i] = parseInt (allvals [i]); Total + = allways [i] & lt; & Lt; 0; Warning (total); }
& lt; Script src = "https://ajax.googleapis.com/ajax/libs/Jquery/1.11.1/jquery.min.js" & gt; & Lt; / Script & gt;
first < / Strong> which you have to create the total variable for your variable, then start it in zero Var total = 0; For (var i = 0; i & lt; allvals.length; i ++) {allvals [i] = parsih (allvals [i], 10); Total + = all Val [i]; Console.log (total); }
Second Because you are adding a number to an initial variable, you are receiving a negative result
For example < / P>
var c; C + = 1; // This nan (will not be numerical number)
The reason for this is that when you initialize a java script, it knows the correct data type that is given to that variable
Comments
Post a Comment