-2
const sampleArray = [
  469,
  755,
  244,
  245,
  758,
  450,
  302,
  20,
  712,
  71,
  456,
  21,
  398,
  339,
  882,
  848,
  179,
  535,
  940,
  472,
];

let mainDiv15 = document.getElementById("main");
let myParagraph15 = document.createElement("p");
let myTextNode15 = document.createTextNode("KATA 16");
myParagraph15.append(myTextNode15);
mainDiv15.append(myParagraph15);
let max14 = 20;
let oneToTwentyFive15 = [];
function array7() {
  sampleArray.forEach((ix) => {
    for (ix = sampleArray; sampleArray <= sampleArray.arrayLength; sampleArray++) 
    mainDiv15.append((ix) + ", ")
  }
}
array7();

Here is my array and here is my function im so darn confused on how to take each element and add them together to get a total sum. any help at all would be greatly appreaited!

brianfuit1
  • 13
  • 4

1 Answers1

0

A simple code is already provided in the comment section. This answer is just to make your code work.

You are using both forEach and inside the callback you are again using for which is not necessary. Both of them will do same thing. If you are using forEach just initialize a variable with 0 and inside the forEach callback add the current number under iteration with the variable.

let sum = 0

function array7() {
  sampleArray.forEach((ix) => {
      sum += ix;
    }
    mainDiv15.append(sum + ", ")
  }
brk
  • 48,835
  • 10
  • 56
  • 78