Skip to content

Instantly share code, notes, and snippets.

@desinas
Last active July 9, 2018 06:13
Show Gist options
  • Save desinas/29c71e51ee856e47a275804e144ec48f to your computer and use it in GitHub Desktop.
Save desinas/29c71e51ee856e47a275804e144ec48f to your computer and use it in GitHub Desktop.
Another type of loop Quiz (6-8) of Udacity FEWD
/*
* Programming Quiz: Another Type of Loop (6-8)
*
* Use the existing `test` variable and write a `forEach` loop
* that adds 100 to each number that is divisible by 3.
*
* Things to note:
* - you must use an `if` statement to verify code is divisible by 3
* - you can use `console.log` to verify the `test` variable when you're finished looping
*/
var test = [12, 929, 11, 3, 199, 1000, 7, 1, 24, 37, 4,
19, 300, 3775, 299, 36, 209, 148, 169, 299,
6, 109, 20, 58, 139, 59, 3, 1, 139
];
// Write your code here
test.forEach(function(number, place, tester) {
if (number%3 === 0) {
tester[place]+= 100;
}
// console.log(number + " at:" + place);// verify the output
});
@desinas
Copy link
Author

desinas commented Dec 21, 2017

What Went Well

  • Your code should have a variable test
  • Your test array should call the forEach() method
  • Your forEach() loop should have an if statement
  • Your code should print correctly modified test

Feedback: Your answer passed all our tests! Awesome job!

@uouoAhmed
Copy link

var test = [12, 929, 11, 3, 199, 1000, 7, 1, 24, 37, 4,
19, 300, 3775, 299, 36, 209, 148, 169, 299,
6, 109, 20, 58, 139, 59, 3, 1, 139
];

test.forEach(function(num,index,array){
if(num %3 === 0){
console.log(num += 100);

}});

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment