From f4ad0419d9ad6bb4f1bbe7b41d257e4822211c1a Mon Sep 17 00:00:00 2001 From: greggubarev Date: Tue, 16 Oct 2018 08:21:07 +0400 Subject: [PATCH] JavaScript: Add hint to Iterate Through an Array with a For Loop (#19217) * JavaScript: Add hint to Iterate Through an Array with a For Loop Add hint to Iterate Through an Array with a For Loop (https://learn.freecodecamp.org/javascript-algorithms-and-data-structures/basic-javascript/iterate-through-an-array-with-a-for-loop and https://guide.freecodecamp.org/certifications/javascript-algorithms-and-data-structures/basic-javascript/iterate-through-an-array-with-a-for-loop) * Update index.md --- .../index.md | 72 +++++++++++++++++-- 1 file changed, 66 insertions(+), 6 deletions(-) diff --git a/client/src/guide/english/certifications/javascript-algorithms-and-data-structures/basic-javascript/iterate-through-an-array-with-a-for-loop/index.md b/client/src/guide/english/certifications/javascript-algorithms-and-data-structures/basic-javascript/iterate-through-an-array-with-a-for-loop/index.md index d52e135bff..2cdbfea636 100644 --- a/client/src/guide/english/certifications/javascript-algorithms-and-data-structures/basic-javascript/iterate-through-an-array-with-a-for-loop/index.md +++ b/client/src/guide/english/certifications/javascript-algorithms-and-data-structures/basic-javascript/iterate-through-an-array-with-a-for-loop/index.md @@ -1,10 +1,70 @@ +--- +title: Iterate Through an Array with a For Loop --- -title: Iterate Through an Array with a For Loop ---- + ## Iterate Through an Array with a For Loop -This is a stub. Help our community expand it. - -This quick style guide will help ensure your pull request gets accepted. - + +Here’s the setup: + +```javascript +// Example +// Example +var ourArr = [ 9, 10, 11, 12]; +var ourTotal = 0; + +for (var i = 0; i < ourArr.length; i++) { + ourTotal += ourArr[i]; +} + +// Setup +var myArr = [ 2, 3, 4, 5, 6]; + +// Only change code below this line +``` + +We need to declare and initialize a variable ```total``` to ```0```. Use a ```for``` loop to add the value of each element of the ```myArr``` array to ```total```. + +We start from declare a variable ```total```: + +```javascript + +// Only change code below this line +var total = 0; +``` + +Next, we need to use a ```for``` loop to add the value of each element of the ```myArr``` array to ```total```: + +```javascript +for (var i = 0; i < myArr.length; i++) { + +} +``` + +Now we need to add each value of ```myArr``` to variable ```total```: + +```javascript +for (var i = 0; i < myArr.length; i++) { + total +=myArr[i]; +} +``` + + Here’s a full solution: + +```javascript +// Example +var ourArr = [ 9, 10, 11, 12]; +var ourTotal = 0; + +for (var i = 0; i < ourArr.length; i++) { + ourTotal += ourArr[i]; +} +// Setup +var myArr = [ 2, 3, 4, 5, 6]; +// Only change code below this line +var total = 0; +for (var i = 0; i < myArr.length; i++) { + total +=myArr[i]; +} +```