diff --git a/.DS_Store b/.DS_Store new file mode 100644 index 00000000..bc0ccb61 Binary files /dev/null and b/.DS_Store differ diff --git a/assignment/scripts/loops-practice.js b/assignment/scripts/loops-practice.js index b4761fdd..b73d7c10 100644 --- a/assignment/scripts/loops-practice.js +++ b/assignment/scripts/loops-practice.js @@ -12,36 +12,48 @@ for (let i=0; i<4; i++) { // 1.a. TODO: Write a for loop to console.log the numbers from 0 to 5 // - Which part of the example loop do you need to change to do this? -console.log('count from 0 to 5'); +for (let i=0; i<6); i++ { + console.log('count from 0 to 5'); +} // 1.b. TODO: Write a for loop to console.log the numbers from 3 to 5 // - Which part of the example loop do you need to change to do this? -console.log('count from 3 to 5'); +for (let i=2; i<6; i++ { + console.log('count from 3 to 5'); +} // 1.c. TODO: Write a for loop to console.log EVEN numbers from 2 to 10 // - Which part of the example loop do you need to change to do this? +for (let i=2; i<=10; +-2 { console.log('count even numbers from 2 to 10 (2, 4, 6, 8, 10):'); +} // 1.d. (STRETCH) TODO: Write a for loop to do a countdown from 5 to 0 -console.log('STRETCH: countdown from 5 to 0'); - +for (let i<6; i=0; --5 { +console.log('STRETCH: countdown from 5 to 0'); +} // 2. For of loops console.log('---- 2. For Of loop ----'); let stars = ['Polaris', 'Gacrux', 'Formalhaut', 'Rigel', 'Deneb'] // 2.a. TODO: Write a for of loop to console.log each star in the 'stars' array -console.log('Some stars:'); - +for (let i=0; i<=5; i++ { +coconnsole.log('Some stars:'); +} // 3. While loops console.log('---- 3. While loop ----'); // 3.a. TODO: Write a while loop to console.log each star in the 'stars' array -console.log('Some stars using while:'); + +console.log(let i>=0; while loop 4) +console.log('Some stars using while:) // 3.b. TODO: Write a while loop to console.log the numbers from 0 to 5 +console.log(let i>=0; while loop 6) console.log('count from 0 to 5'); // 3.c. TODO: Write a while loop to console.log the numbers from 10 to 5 +console.log(let i <=10; while loop 5) console.log('count backwards from 10 to 5'); diff --git a/loops-practice.js b/loops-practice.js new file mode 100644 index 00000000..12413e28 --- /dev/null +++ b/loops-practice.js @@ -0,0 +1,66 @@ +console.log('****** Loops Practice *******'); + + +// 1. 'for' loop +console.log('---- 1. For loops ----'); +// Example: a for loop to console.log numbers from 0 to 3 +console.log('count from 0 to 3'); +// start i at 0, while i is < 4 do code between {}, afterwards add 1 to i (i++) +for (let i=0; i<4; i++) { + console.log('count from 0 to 3:'); +} + +// 1.a. TODO: Write a for loop to console.log the numbers from 0 to 5 +// - Which part of the example loop do you need to change to do this? +for (let i=0; i<6; i++) { +console.log('count from 0 to 5'); +} + +// 1.b. TODO: Write a for loop to console.log the numbers from 3 to 5 +// - Which part of the example loop do you need to change to do this? +for (let i=3; i<6; i++) +console.log('count from 3 to 5'); + +// 1.c. TODO: Write a for loop to console.log EVEN numbers from 2 to 10 +// - Which part of the example loop do you need to change to do this? +for (let i=2; i<11; i+-2) { +console.log('count even numbers from 2 to 10 (2, 4, 6, 8, 10):'); +} +// 1.d. (STRETCH) TODO: Write a for loop to do a countdown from 5 to 0 +for (let i=5; i>-1; i--) { +console.log('STRETCH: countdown from 5 to 0'); +} + +// 2. For of loops +let stars = ['Polaris', 'Gacrux', 'Formalhaut', 'Rigel', 'Deneb'] +for (let star of stars) { + i>=0; i<=4; i+- +console.log(star); +} + + +// 2.a. TODO: Write a for of loop to console.log each star in the 'stars' array +for (let i=0; i<5; i++) { + console.log('stars[i]'); +} +// 3. While loops +while (i<5) { + i>=0; i<=4; i+- + cobsole.log('stars[i]'); +} +console.log('----3. while loops ----'); + +// 3.a. TODO: Write a while loop to console.log each star in the 'stars' array +while (i<5) + i>=0; i<=4; i+- +console.log('Some stars using while:'); + +// 3.b. TODO: Write a while loop to console.log the numbers from 0 to 5 +while (i<6) + i>=0; i<=5; i+- +console.log('count from 0 to 5'); + +// 3.c. TODO: Write a while loop to console.log the numbers from 10 to 5 +while (i>4) + i>=10; i>4; i-- +console.log('count backwards from 10 to 5');