-1

I am relatively new to coding. I am working on arrays. I am trying to add within an array. I have 6 different track lengths and I want my output to be the total of all six tracks. How do I combine information within my array?

ThS
  • 4,597
  • 2
  • 15
  • 27
  • 2
    Add snippet of your array and desired result. – timbersaw Feb 28 '21 at 14:15
  • 1
    what programming language? – ATP Feb 28 '21 at 14:25
  • let trackTimes = [181, 213, 195, 153, 288, 203, 174]; ////////////////////////////////////////////////////// // Do not change anything above here. ////////////////////////////////////////////////////// /* Update the `albumRuntime` variable to be the sum value of all items in the `trackTimes` array. Use bracket notation to access each value of the `trackTimes` array. Then add all of those values together. */ albumRuntime = trackTimes; – Adam Bloomberg Feb 28 '21 at 17:08
  • I am working in Java Script. My goal is to have a total sum for the entire array – Adam Bloomberg Feb 28 '21 at 17:09
  • tevemadar's answer is pretty sufficient for a beginner. But just FYI, you can also do `const totalTrackTime = trackTimes.reduce((c, it) => c + it)`, which would return the same answer using the functional `.reduce()` API. – Daniel Cheung Feb 28 '21 at 18:30

1 Answers1

0

Based on the comment...

let trackTimes = [181, 213, 195, 153, 288, 203, 174];
//////////////////////////////////////////////////////
// Do not change anything above here. > 
//////////////////////////////////////////////////////
/* Update the albumRuntime variable to be the sum value of all items in the trackTimes
array. Use bracket notation to access each value of the trackTimes array. Then
add all of those values together. */

...the most classic way is expected to be used here: loop an index variable over the length of the array, and use trackTimes[index] to access each element, adding it to the sum albumRuntime, which should be started with a zero:

let trackTimes = [181, 213, 195, 153, 288, 203, 174];
//////////////////////////////////////////////////////
// Do not change anything above here. > 
//////////////////////////////////////////////////////

let albumRuntime = 0;
for(let index = 0; index < trackTimes.length; index++) {
  albumRuntime = albumRuntime + trackTimes[index];
}
console.log(albumRuntime);

For a rather large collection of array-summing snippets with different techniques, see How to find the sum of an array of numbers

Side remark: it's better to add such details directly intto the question itself, there is an Edit button/link just below it.

tevemadar
  • 12,389
  • 3
  • 21
  • 49