Skip to content

Instantly share code, notes, and snippets.

@kimschles
Created March 4, 2019 02:10
Show Gist options
  • Select an option

  • Save kimschles/2a29cabdcca5721ea2c4deb429f849e8 to your computer and use it in GitHub Desktop.

Select an option

Save kimschles/2a29cabdcca5721ea2c4deb429f849e8 to your computer and use it in GitHub Desktop.
loop-array.js from CJ's accelerator
// 1. Write a for loop that will log each element in the following array
let instructors = ['CJ', 'Casey', 'Kim', 'Kyle'];
// 2. Write a for loop that will log each element in the following array
let numbers = [4, 8, 15, 16, 23, 42];
// 3. Write a for loop that will log each element in the following array
let animals = ['dog', 'cat', 'bird', 'mouse'];
// 4. Write a for loop that will log each element in the following array
let books = ['JavaScript: The Good Parts', 'Eloquent JS', 'You Don\'t Know JS'];
// 5. Refactor the code below to use a for loop
let sum = 0;
let values = [4, 8, 2, 7, 3, 9];
let i = 0;
sum += values[i];
i++;
sum += values[i];
i++;
sum += values[i];
i++;
sum += values[i];
i++;
sum += values[i];
i++;
sum += values[i];
console.log(sum);
// 6. Refactor the code below to use a for loop
let sentence ='';
let words = ['Hello', 'World', 'Today'];
let j = 0;
sentence += words[j] + ' ';
j++;
sentence += words[j] + ' ';
j++;
sentence += words[j] + ' ';
console.log(sentence);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment