Skip to content

Instantly share code, notes, and snippets.

@youngjinmo
Last active November 17, 2018 03:08
Show Gist options
  • Save youngjinmo/39d5fcaa89405d4a9ffdb5f64b37cc74 to your computer and use it in GitHub Desktop.
Save youngjinmo/39d5fcaa89405d4a9ffdb5f64b37cc74 to your computer and use it in GitHub Desktop.
Comparison of Loop and forEach Loop
/*
* 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
// function myTest(test){
// for(var i=0; i<test.length; i++){
// if(test[i]%3===0){
// test[i] += 100;
// }
// console.log(test);
// }
// }
// myTest(test);
test.forEach(function(element, index, array) {
if (element%3===0) {
array[index] += 100;
}
console.log(test); // you can parameters 'element', 'array' replaces to 'test'
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment