This question already has answers here:
How to compute the sum and average of elements in an array? [duplicate]
(35 answers)
Closed 10 months ago.
I tried to iterate through the array and print the sum. But the output am getting is elements of the array.
<p id="ans"></p>
<script>
var text = "the mean is ";
function mean() {
var sum = 0;
var input = document.getElementsByName("values");
for (var i = 0; i < input.length; i++) {
sum += input[i].value;
text = text + sum;
}
document.getElementById("ans").innerHTML = text;
}
</script>
Parse the string values into integers and then sum it.
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/parseInt
This is because in javascript if you adding a number to a string it will be casted to a string. For example:
0 + '1' + '2' // 012
But with parse int:
0 + parseInt('1') + parseInt('2') // 3
Or you can cast to int with a simple plus also:
0 + (+'1') + (+'2') // 3
This question already has answers here:
Using Javascript to compare two input numbers in HTML?
(4 answers)
issue with comparing two numbers in javascript
(5 answers)
compare two input values in input validation html javascript?
(3 answers)
Closed 3 years ago.
I'm writing my first javascript/html code to collect football results and then produce a league table. My code copes with single digit scores fine but not double digit scores.
Three arrays are used for results, league and players as below:
Results=[];
var Results=[
["Home","F","A","Away"],
["Si",,,"Ste"],
["Si",,,"Gaz"],
["Ste",,,"Gaz"],
["Ste",,,"Si"],
["Gaz",,,"Si"],
["Gaz",,,"Ste"],
["Si",,,"Ste"],
["Si",,,"Gaz"],
["Ste",,,"Gaz"],
["Ste",,,"Si"],
["Gaz",,,"Si"],
["Gaz",,,"Ste"],
];
League=[];
var League=[
["Team","P","W","D","L","F","A","GD","Pts"],
["Si",,,,,,,,],
["Ste",,,,,,,,],
["Gaz",,,,,,,,]
];
players=[];
var players=["Si","Ste","Gaz"];
I believe the faulty code is where wins are calculated:
if (Results[i][1]>Results[i][2])
{ wins++;
pts= +pts + 3;
}
This is taken from two 'for' loops that iterate through the results array for each player and populate the league array accordingly:
for (p = 0; p < players.length; p++)
{
for (i = 1; i < Results.length; i++)
{
if (Results[i][1]!= "")
{
if (Results[i][0]==players[p])
{
pld++;
goalsF=+goalsF + +Results[i][1];
goalsA=+goalsA + +Results[i][2];
gd=(goalsF - goalsA);
if (Results[i][1]>Results[i][2])
{
wins++;
pts= +pts + 3;
}
else if (Results[i][1]<Results[i][2])
{
loses++;
}
else
{
draws++;
pts++
}
}
}
if (Results[i][1]!= "")
{
if (Results[i][3]==players[p])
{
pld++;
goalsF=+goalsF + +Results[i][2];
goalsA=+goalsA + +Results[i][1];
gd=(goalsF - goalsA);
if (Results[i][1]<Results[i][2])
{
wins++;
pts= +pts + 3;
}
else if (Results[i][1]>Results[i][2])
{
loses++;
}
else
{
draws++;
pts++
}
}
}
}
r=p+1;
League[r][1]=pld;
League[r][2]=wins;
League[r][3]=draws;
League[r][4]=loses;
League[r][5]=goalsF;
League[r][6]=goalsA;
League[r][7]=gd;
League[r][8]=pts;
var pld=0;
var wins=0;
var draws=0;
var loses=0;
var goalsF=0;
var goalsA=0;
var gd=0;
var pts=0;
var r=0;
}
For a 10-1 score it works correctly:
For 10-2 (or 10-3) the result gets reversed and the player with the lower score is deemed the winner!?
It's as if the comparison is only working on the first digit of the scoreline. How do I fix this?
This question already has answers here:
How to find the sum of an array of numbers
(59 answers)
Closed 6 years ago.
I'm trying to find a way to sum all the numbers that have been added to an array. I believe this should work:
var total = 0;
for (var i = 0; i < totalPrice.length; i++);
total += totalPrice[i];
document.getElementById("displayPrice").innerHTML = total;
But total comes out as a NaN.
This is an example of my code in JSFiddle, if you add the item twice the value gets pushed into the array, what am I doing wrong with the for loop?
https://jsfiddle.net/bgv5s9re/2/
You could use brackets
var total = 0;
for (var i = 0; i < totalPrice.length; i++){
total += totalPrice[i];
}
document.getElementById("displayPrice").innerHTML = total;
This question already has answers here:
JavaScript Number Split into individual digits
(30 answers)
Closed 6 years ago.
For instance, if i have the number 4444. I need it to do 4+4+4+4=16.
i want to get the result of 16. I tried changing the number to a string and got the array. But i don't know how to add it after. I searched it up but the other examples were too complicated for my level.
Working Example
var n = 4444;
var a = n.toString().split('').map(Number).reduce(function(a, b) {
return a + b;
});
Turn number to string
Split it
Map over array to return Numbers
Reduce to get total
or with a loop:
var sum = 0;
var string = n.toString();
for (var i = 0; i < string.length; i++) {
sum = sum + Number(string[i]);
}
This question already has answers here:
How to avoid scientific notation for large numbers in JavaScript?
(27 answers)
Closed 6 years ago.
e.g.Number.MAX_VALUE.toString() is "1.7976931348623157e+308"
I hope there is no e+308,How to achieve this ?
You could do like this:
var n = Number.MAX_VALUE.toString();
var parts = n.split("e+");
var first = parts[0].replace('.', "");
var zeroes = parseInt(parts[1], 10) - (first.length - 1);
for(var i = 0; i < zeroes; i++){ first += "0"; }
// => first === "179769313486231570000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000"