Tried converting Jquery Values but still get Nan error

JavaScript and client side scripting.

Moderator: General Moderators

Post Reply
ShatterStar
Forum Newbie
Posts: 4
Joined: Sun Nov 22, 2015 9:19 am

Tried converting Jquery Values but still get Nan error

Post 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.
ShatterStar
Forum Newbie
Posts: 4
Joined: Sun Nov 22, 2015 9:19 am

Re: Tried converting Jquery Values but still get Nan error

Post 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?
Post Reply