From 5578f5b23ae2070104455c1fef7d691915b0af85 Mon Sep 17 00:00:00 2001 From: Dane Middleton Date: Fri, 11 May 2018 13:56:16 -0500 Subject: [PATCH 1/3] create loops.js initial commit --- 04week/loops.js | 1 + 1 file changed, 1 insertion(+) create mode 100644 04week/loops.js diff --git a/04week/loops.js b/04week/loops.js new file mode 100644 index 000000000..ccacec309 --- /dev/null +++ b/04week/loops.js @@ -0,0 +1 @@ +'use strict' From 1d9e7c28d64ada2e825f8786aaf8cdd92581b195 Mon Sep 17 00:00:00 2001 From: Dane Middleton Date: Mon, 14 May 2018 12:21:12 -0500 Subject: [PATCH 2/3] loops.js file with loops and questions complete --- 04week/loops.js | 75 +++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 75 insertions(+) diff --git a/04week/loops.js b/04week/loops.js index ccacec309..677ff96ea 100644 --- a/04week/loops.js +++ b/04week/loops.js @@ -1 +1,76 @@ 'use strict' + + +// for loop +// Use a for loop to console.log each item in the array carsInReverse. + + +const carsInReverse = ['lebaron', 'concorde', 'wrangler']; + + +// for (let i = 0; carsInReverse.length > i; i++) { +// console.log(carsInReverse[i]); +// } + + +// for...in loop +// Create an object (an array with keys and values) called persons with the following data: +// firstName: "Jane" +// lastName: "Doe" +// birthDate: "Jan 5, 1925" +// gender: "female" + + +const persons = { + firstName: "Jane", + lastName: "Doe", + birthDate: "Jan 5, 1925", + gender: "female" +}; + + +// Use a for...in loop to console.log each key. + + +// for (let x in persons) { +// console.log(x); +// } + +// Then use a for...in loop and if state to console.log the value associated with the key birthDate. + + +// for (let x in persons) { +// if (x === 'birthDate') { +// console.log(persons[x]) +// } +// } + + +// while loop +// Use a for loop to console.log the numbers 1 to 1000. + + +// for(let i = 1; i <= 1000; i++) { +// console.log(i) +// } + +// do...while loop +// Use a do...while loop to console.log the numbers from 1 to 1000. + + +// let i = 1; +// +// do { +// console.log(i), i++ +// } +// while (i <= 1000) + + +// When is a for loop better than a while loop? +// when you want to check a condition AND the number of iterations is known and you want to initialize a variable and increase/decrease the variable. + +// How is the readability of the code affected? +// while loops indicate checking condition/s. for loops check conditions while increasing or decreasing a variable. + +// What is the difference between a while loop and a do...while loop? +// the while loop checks if the condition is true before executing the code block. the do..while loop executes the code block once before checking if the condition is true. From e6aafb47c0a42232606b690248abb7e0dd22ef0d Mon Sep 17 00:00:00 2001 From: Dane Middleton Date: Tue, 15 May 2018 22:39:54 -0500 Subject: [PATCH 3/3] update with while loop to console.log the numbers 1 to 1000 --- 04week/loops.js | 20 +++++++++++++------- 1 file changed, 13 insertions(+), 7 deletions(-) diff --git a/04week/loops.js b/04week/loops.js index 677ff96ea..485339a23 100644 --- a/04week/loops.js +++ b/04week/loops.js @@ -5,7 +5,7 @@ // Use a for loop to console.log each item in the array carsInReverse. -const carsInReverse = ['lebaron', 'concorde', 'wrangler']; +// const carsInReverse = ['lebaron', 'concorde', 'wrangler']; // for (let i = 0; carsInReverse.length > i; i++) { @@ -21,12 +21,12 @@ const carsInReverse = ['lebaron', 'concorde', 'wrangler']; // gender: "female" -const persons = { - firstName: "Jane", - lastName: "Doe", - birthDate: "Jan 5, 1925", - gender: "female" -}; +// const persons = { +// firstName: "Jane", +// lastName: "Doe", +// birthDate: "Jan 5, 1925", +// gender: "female" +// }; // Use a for...in loop to console.log each key. @@ -47,6 +47,12 @@ const persons = { // while loop +let x = 0; +while (x < 1000) { + x++; + console.log(x) +} + // Use a for loop to console.log the numbers 1 to 1000.