Heads up! To view this whole video, sign in with your Courses account or enroll in your free 7-day trial. Sign In Enroll
Preview
Start a free Courses trial
to watch this video
Let's walkthrough the solution to the first code challenge.
This video doesn't have any notes.
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
For the first challenge, we need to
use the forEach method to add together
0:00
all of the elements in the numbers array,
and
0:04
store the total in
the variable named total.
0:07
Before I show you how I solve this
code challenge, I'm going to copy and
0:10
paste the code for this challenge
into GitHub's Atom text editor.
0:14
Doing this will make
the code easier to read.
0:20
To solve this challenge, I started by
adding the call to the forEach method.
0:24
We wanna iterate over the numbers array,
so I'll type numbers.forEach.
0:28
Then I added the anonymous call back
function using error function syntax.
0:36
Since we're iterating
over a list of numbers,
0:41
I named the current value parameter,
number, followed by a fat arrow.
0:44
For the function body you could add
a code block like I just did here.
0:50
Or, you could just write the expression
to add each number to the total variable.
0:56
There are a couple of ways to add
the number to the total variable.
1:01
You could add the total variable
to the number parameter and
1:04
assign the result back to
the total variable like this.
1:08
Total equals total plus number.
1:11
Or, you could use the addition assignment
operator to make this code more concise.
1:16
Total plus equals number.
1:24
That's the approach that I ended up
using and that completes this challenge.
1:29
So how did you do?
1:34
Don't worry if your solution
didn't match mine exactly.
1:36
There's definitely more than one way
to correctly solve this challenge.
1:39
See you after the next challenge.
1:43
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