From 7b8be133f0b95822671bd71ffb5afbcc140b8517 Mon Sep 17 00:00:00 2001 From: Shay Hoffman Date: Wed, 14 Feb 2018 18:49:46 -0600 Subject: [PATCH 1/5] adding loop file --- 04week/loop.js | 1 + 1 file changed, 1 insertion(+) diff --git a/04week/loop.js b/04week/loop.js index e69de29bb..ccacec309 100644 --- a/04week/loop.js +++ b/04week/loop.js @@ -0,0 +1 @@ +'use strict' From 30eb58c1739b0d254bb0db2ffe7ddeb756825d39 Mon Sep 17 00:00:00 2001 From: Shay Hoffman Date: Wed, 14 Feb 2018 19:06:49 -0600 Subject: [PATCH 2/5] first loop --- 04week/loop.js | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/04week/loop.js b/04week/loop.js index ccacec309..605adb438 100644 --- a/04week/loop.js +++ b/04week/loop.js @@ -1 +1,9 @@ 'use strict' + +//Use a for loop to console.log each item in the array carsInReverse +const carsInReverse = ['honda', 'toyota', 'bmw',' jeep', 'ford', 'tesla'] +for(let i = 0; i < carsInReverse.length; i++) { + console.log(carsInReverse[i]); +} + +//Create an object (an array with keys and values) called persons with the following data: From 1f7848a523c4767e031ae8ea753ee3071db83334 Mon Sep 17 00:00:00 2001 From: Shay Hoffman Date: Wed, 14 Feb 2018 19:15:44 -0600 Subject: [PATCH 3/5] second loop --- 04week/loop.js | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/04week/loop.js b/04week/loop.js index 605adb438..789a04938 100644 --- a/04week/loop.js +++ b/04week/loop.js @@ -7,3 +7,13 @@ for(let i = 0; i < carsInReverse.length; i++) { } //Create an object (an array with keys and values) called persons with the following data: +let persons = { + firstName: "Jane", + lastName: "Doe", + birthDate: "Jan 5 1925", + gender: "female", +} + +for (let x in persons) { + console.log(x); +} From 8f86151bc7ffd0bf5b0279d50979609c4b21d4de Mon Sep 17 00:00:00 2001 From: Shay Hoffman Date: Wed, 14 Feb 2018 19:32:07 -0600 Subject: [PATCH 4/5] second part of second loop and third loop --- 04week/loop.js | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/04week/loop.js b/04week/loop.js index 789a04938..4241f6ab9 100644 --- a/04week/loop.js +++ b/04week/loop.js @@ -17,3 +17,13 @@ let persons = { for (let x in persons) { console.log(x); } + +for (let x in persons) { + if(x === 'birthDate'){ + console.log(persons[x]); + } +} + +for(let i = 0; i < 1001; i++) { + console.log(i) +} From 484c6c8085ec8b413ec51255f127f734ca5b9763 Mon Sep 17 00:00:00 2001 From: Shay Hoffman Date: Wed, 14 Feb 2018 20:00:39 -0600 Subject: [PATCH 5/5] last loop and questions --- 04week/loop.js | 27 +++++++++++++++++++++++++-- 1 file changed, 25 insertions(+), 2 deletions(-) diff --git a/04week/loop.js b/04week/loop.js index 4241f6ab9..b2c9c26f4 100644 --- a/04week/loop.js +++ b/04week/loop.js @@ -24,6 +24,29 @@ for (let x in persons) { } } -for(let i = 0; i < 1001; i++) { - console.log(i) + +//Use a while loop to console.log the numbers 1 to 1000. +let number = 1; +while (number < 1001) { + console.log(number); + number++; } + +//Use a do...while loop to console.log the numbers from 1 to 1000 +let number = 0; +do { + number ++; + console.log(number); +} while (number < 1001); + +//When is a for loop better than a while loop? +//You would use a for loop when you know how many times the loop is going to iterate through the object and a while loop when you don't know or aren't sure. + +//How is the readability of the code affected? +//It seems like the readability depends on what sort of object you're trying to work with. If you have an object that has the variable defined outside the loop, a while loop would be the better choice. However, maybe it's because we've spent more time with for loops, but they seem generally more readable to me because the syntax is more intuitive. + +//What is the difference between a for loop and a for...in loop? +//For in loops are for object only. For loops are for arrays. + +//What is the difference between a while loop and a do...while loop? +//a while loop will perform the blocl of code for as long as a specified condition is true, and a do while loop will always execute once, even if the condition is never true.