Page 1 of 1

Tried converting Jquery Values but still get Nan error

Posted: Thu Dec 17, 2015 8:25 am
by ShatterStar

Code: Select all

	newArray = [];
	function AddAnotherExpense(detail, amount){	
		var newArray2 = { "detail" : detail, "amount" : amount };
		newArray.push(newArray2);
		//console.log(newArray);
	};

	total = 0;
	$("input[name=addAnotherExpense]").on("click", function(e){
		e.preventDefault();
		var expenseDetail = $("input[name=expenseDetail]").val();
		var expenseAmount = parseFloat($("input[name=expenseAmount]").val());
		AddAnotherExpense(expenseDetail, expenseAmount);
		
		for(i=0; i<newArray.length;i++)
		{	
			amount = parseFloat(newArray[i].amount);
			total = parseFloat(total) + amount;
			console.log("total in for loop = " + total);
		}
		$("#totalExpensesParagraph").append(total);
	});
Hi guys please view the above jquery code, I am converting the input into a float using parseFloat in 3 different places but upon logging the output to the console I am still getting the error "total in for loop = NaN". I just want the totals in the object added and not be a string concatenation.

Re: Tried converting Jquery Values but still get Nan error

Posted: Thu Dec 17, 2015 9:55 am
by ShatterStar
ok guys what I did was put the parsefloat on the actual amount input element and that solved the NaN problem, but my calculations are still off somehow, any ideas?