Reputation: 41
I figured this next step would be simple but I've been pulling my hair out trying to figure it out. I am putting in an amount of money spent and inputting it via a submit button. As I am putting in multiple values (since I'm also making a list of what the money was spent on) I am trying to get a total of all the money spent, but I either get NaN or just nothing. In theory I am hoping that every time I would hit confirm, the amount of money in the text box would be added to the total for that day.
I've been trying to push the value in an array, then summing it with a for loop but perhaps I'm making this more complicated than it needs to be. Here's my code:
function totalSpent() {
var total= 0;
var spent = document.getElementById("moneySpent").value;
var array = [];
// check if the entered value is number
if (isNaN(Number(spent))) {
alert("Numbers only");
} else {
var spent = parseInt(document.getElementById("moneySpent").value);
for (var i=0; i<spent.length;++i){
array.push(parseFloat(spent[i].value));
}
total = array.reduce(function(previousValue, currentValue, index, array){
return previousValue + currentValue;
});
youSpent.innerHTML=`Total Spent Today: ${total}`;
}
}
<input type="amount" size=25 Placeholder="How much did you spend?"
id="moneySpent">
<input type="button" value="confirm" onClick="totalSpent()">
<br>
<h2 id="youSpent"> Total Spent Today:
</h2>
Upvotes: 0
Views: 97
Reputation: 21
<input type="amount" size=25 Placeholder="How much did you spend?" id="moneySpent">
<input type="button" value="confirm" onClick="totalSpent()">
<br>
<h2 id="youSpent"> Total Spent Today:
var total = 0;
var array = [];
function totalSpent() {
var spent = document.getElementById("moneySpent").value;
// check if the entered value is number
if (isNaN(Number(spent))) {
alert("Numbers only");
} else {
var spent = parseFloat(document.getElementById("moneySpent").value);
array.push(spent);
total += spent;
document.getElementById('youSpent').innerHTML=`Total Spent Today: ${total}`;
}
}
Upvotes: 0
Reputation: 1032
Here you are my friend, I've made minimal changes to make your code work:
array.push(spent)
one time, no loop neededarray.reduce
var array = [];
function totalSpent() {
var total= 0;
var spent = document.getElementById("moneySpent").value;
// check if the entered value is number
if (isNaN(Number(spent))) {
alert("Numbers only");
} else {
var spent = parseInt(document.getElementById("moneySpent").value);
array.push(spent);
total = array.reduce(function(previousValue, currentValue, index, array){
return previousValue + currentValue;
}, 0);
youSpent.innerHTML=`Total Spent Today: ${total}`;
}
}
<input type="amount" size=25 Placeholder="How much did you spend?"
id="moneySpent">
<input type="button" value="confirm" onClick="totalSpent()">
<br>
<h2 id="youSpent"> Total Spent Today:
</h2>
Upvotes: 1
Reputation: 305
So, there are 2 things i saw, the first one:
var spent = parseInt(document.getElementById("moneySpent").value);
When you transform your string input value to "parseInt" you lost the String.length property, so your for() won't work.
Second, if you gonna sum all numbers, i dont see the need of push all values to one array then loop it and sum the number, you could do something like:
} else {
total = 0;
var spent = document.getElementById("moneySpent").value;
for (var i=0; i<spent.length;++i) total += parseInt(spent[i]);
youSpent.innerHTML=`Total Spent Today: ${total}`;
}
Upvotes: 0
Reputation: 116
You have to declare your array out of the function otherwise, each time the function is called, you erased the old values
https://jsfiddle.net/c3L5q6pa/
var array = [];
function totalSpent() {
var spent = parseFloat(document.getElementById("moneySpent").value);
// check if the entered value is number
if (isNaN(spent)) {
return alert("Numbers only");
}
array.push(spent)
const total = array.reduce((previous, current) => previous + current);
youSpent.innerHTML=`Total Spent Today: ${total}`;
})
Upvotes: 0