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
This commit is contained in:
committed by
Quincy Larson
parent
09aa755e05
commit
f4ad0419d9
@ -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
|
## Iterate Through an Array with a For Loop
|
||||||
|
|
||||||
This is a stub. <a href='https://github.com/freecodecamp/guides/tree/master/src/pages/certifications/javascript-algorithms-and-data-structures/basic-javascript/iterate-through-an-array-with-a-for-loop/index.md' target='_blank' rel='nofollow'>Help our community expand it</a>.
|
|
||||||
|
|
||||||
<a href='https://github.com/freecodecamp/guides/blob/master/README.md' target='_blank' rel='nofollow'>This quick style guide will help ensure your pull request gets accepted</a>.
|
|
||||||
|
|
||||||
<!-- The article goes here, in GitHub-flavored Markdown. Feel free to add YouTube videos, images, and CodePen/JSBin embeds -->
|
<!-- The article goes here, in GitHub-flavored Markdown. Feel free to add YouTube videos, images, and CodePen/JSBin embeds -->
|
||||||
|
|
||||||
|
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];
|
||||||
|
}
|
||||||
|
```
|
||||||
|
Reference in New Issue
Block a user