Heads up! To view this whole video, sign in with your Courses account or enroll in your free 7-day trial. Sign In Enroll
Start a free Courses trial
to watch this video
Use the for
loop to iterate over an array of song names and display them on a web page.
Get the average of an array of scores
In the following example, a for
loop accesses each number in the scores
array and adds it to the current value of total
. When the loop completes, the console displays the average score by dividing the value of total
by the length of the scores
array.
const scores = [ 20, 50, 75, 100, 115 ];
let total = 0;
for ( let i = 0; i < scores.length; i++ ) {
total += scores[i];
}
console.log( total / scores.length );
Resources
Related Discussions
Have questions about this video? Start a discussion with the community and Treehouse staff.
Sign upRelated Discussions
Have questions about this video? Start a discussion with the community and Treehouse staff.
Sign up
You need to sign up for Treehouse in order to download course files.
Sign upYou need to sign up for Treehouse in order to set up Workspace
Sign up