Reputation: 33
I'm trying to make something like this and i cant get it right how many tries i do. i have tried so many ways and at the moment i have got it to show the sum of the items but it skips the name in the beginning of every nested array.
00-08 08-16 16-24 medel
Malmö 12 16 9 12.3
Mariestad 13 15 10 12.7
Stockholm 13 15 13 13.7
Upphärad 14 16 15 15
Göteborg 13 14 12 13
var tempen = [
["", "00-08", "08-16", "16-24"],
["Malmö", 12, 16, 9],
["Mariestad", 13, 15, 10],
["Stockholm", 13, 15, 13],
["Upphärad", 14, 16, 15],
["Göteborg", 13, 14, 12]
];
var newArray = tempen.slice();
tempen[0].push("medel");
function sum_value(prev_value, next_value, ) {
return ( prev_value + next_value);
};
for (var i = 1; i < newArray.length; i++) {
newArray[i].shift();
var sum = newArray[i].reduce(sum_value);
newArray[i].push(sum);
}
function printit() {
var varMyinnerHTML = "<table>";
for (var i = 0; i < tempen.length; i++) {
varMyinnerHTML += "<tr>";
for (var j = 0; j < tempen[i].length; j++) {
varMyinnerHTML += "<td>" + tempen[i][j] + "</td>";
}
varMyinnerHTML += "</tr>";
}
document.getElementById("kontainer").innerHTML = varMyinnerHTML;
}
any help would be awesome im really bad at this :(
Upvotes: 0
Views: 77
Reputation: 23372
You were nearly there! Here's your code with minimal modifications for it to work. The fixes are described in the comments.
The main fixes:
but it skips the name in the beginning of every nested array
When calculating the sum, you had to remove the place name string
to make sure you don't end up with NaN
. However, you did so by using shift
, which removed it from the original array you still needed to render the table. To create a new array of just the numbers, you can use slice
. This doesn't mutate the original array so you can later still access the place name.
i have got it to show the sum of the items
You were only pushing the sum
, but forgot to divide by the number of values!
You forgot to close your <table>
tag
var tempen = [
["", "00-08", "08-16", "16-24"],
["Malmö", 12, 16, 9],
["Mariestad", 13, 15, 10],
["Stockholm", 13, 15, 13],
["Upphärad", 14, 16, 15],
["Göteborg", 13, 14, 12]
];
// We don't need a new array
tempen[0].push("medel");
function sum_value(prev_value, next_value) {
return (prev_value + next_value);
};
for (var i = 1; i < tempen.length; i++) {
// Don't delete the place names from the orginal!
// newArray[i].shift();
// Only skip the place name for the sum
var numbers = tempen[i].slice(1);
var sum = numbers.reduce(sum_value);
// Here, we compute the average by dividing
// sum by length:
var avg = sum / numbers.length;
// Push the average, not the sum:
tempen[i].push(avg);
}
function printit() {
// Add a body
var varMyinnerHTML = "<table><tbody>";
for (var i = 0; i < tempen.length; i++) {
varMyinnerHTML += "<tr>";
for (var j = 0; j < tempen[i].length; j++) {
varMyinnerHTML += "<td>" + tempen[i][j] + "</td>";
}
varMyinnerHTML += "</tr>";
}
// Close the body and table
varMyinnerHTML += "</tbody></table>"
document.getElementById("kontainer").innerHTML = varMyinnerHTML;
}
// Run the code
printit();
<div id="kontainer"></div>
Upvotes: 1
Reputation: 5488
Simply use forEach
for iterating. Nothing is different with arrays and nested arrays.
var tempen = [
["", "00-08", "08-16", "16-24"],
["Malmö", 12, 16, 9],
["Mariestad", 13, 15, 10],
["Stockholm", 13, 15, 13],
["Upphärad", 14, 16, 15],
["Göteborg", 13, 14, 12]
],
newArr = [];
tempen.forEach(function(itm){
let sum = 0;
itm.forEach(function(num){
if(typeof num == 'number') {
sum += num;
}
});
newArr.push(sum/(itm.length - 1));
});
printit();
function printit() {
var varMyinnerHTML = "<table>";
for (var i = 0; i < tempen.length; i++) {
varMyinnerHTML += "<tr>";
for (var j = 0; j < tempen[i].length; j++) {
varMyinnerHTML += "<td>" + tempen[i][j] + "</td>";
}
varMyinnerHTML += "<td>" + newArr[i] + "</td>";
varMyinnerHTML += "</tr>";
}
document.getElementById("kontainer").innerHTML = varMyinnerHTML;
}
<div id="kontainer"></div>
Upvotes: 0