From 6799d089cb954df09067825b6e0da87a50db8a81 Mon Sep 17 00:00:00 2001 From: Abby Gottlich Date: Sat, 3 Nov 2018 11:27:15 -0500 Subject: [PATCH 1/6] for loop carsInReverse --- 04week/loop.js | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/04week/loop.js b/04week/loop.js index e69de29bb..a6b633741 100644 --- a/04week/loop.js +++ b/04week/loop.js @@ -0,0 +1,8 @@ +// Use a for loop to console.log each item in the array carsInReverse. + +const carsInReverse = ["honda", "nissan", "toyota", "volkswagon", "chevy"]; + +// Starting at 0, loop through every item in the array up to the length of the array. Then print each item(i) in that array. +for (let i = 0; i <= carsInReverse.length; i++) { + console.log(carsInReverse[i]); +} From afa0fc2d7f8bb486afcfa43eb198f1c67cd8b918 Mon Sep 17 00:00:00 2001 From: Abby Gottlich Date: Sat, 3 Nov 2018 11:28:58 -0500 Subject: [PATCH 2/6] for...in loop persons keys --- 04week/loop.js | 27 +++++++++++++++++++++++++++ 1 file changed, 27 insertions(+) diff --git a/04week/loop.js b/04week/loop.js index a6b633741..c9d605313 100644 --- a/04week/loop.js +++ b/04week/loop.js @@ -6,3 +6,30 @@ const carsInReverse = ["honda", "nissan", "toyota", "volkswagon", "chevy"]; 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: +// firstName: "Jane" +// lastName: "Doe" +// birthDate: "Jan 5, 1925" +// gender: "female" +// Use a for...in loop to console.log each key. + +// Create a variable that is equal to an emtpy string. This will be used to pass info into. +let infoStr = ""; + +const persons = { + firstName: "Jane ", + lastName: "Doe ", + birthDate: "Jan 5, 1925 ", + gender: "female" +}; + +// Create a variable that creates an array of all of the items' keys. +const personsKeys = Object.keys(persons); + +// Loop through the array, and for every item, add it to the empty string variable. +for (let item in personsKeys) { + infoStr += personsKeys[item]; +} + +console.log(infoStr); From 35197e53afc044192dcaf05b5d1dd9a6dc8eeee3 Mon Sep 17 00:00:00 2001 From: Abby Gottlich Date: Sat, 3 Nov 2018 11:30:19 -0500 Subject: [PATCH 3/6] for...in loop and if statement birthDate value --- 04week/loop.js | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/04week/loop.js b/04week/loop.js index c9d605313..4dfbea15b 100644 --- a/04week/loop.js +++ b/04week/loop.js @@ -33,3 +33,12 @@ for (let item in personsKeys) { } console.log(infoStr); + +// Then use a for...in loop and if state to console.log the value associated with the key birthDate. + +// For every item in the persons object, check to see if it's titled "birthDate". If the object does include an item titled birthDate, print out the value of that key. +for (let item in persons) { + if (item == "birthDate") { + console.log(persons.birthDate); + } +} From 963651427439d350bd54cc9093714b174c2398bc Mon Sep 17 00:00:00 2001 From: Abby Gottlich Date: Sat, 3 Nov 2018 11:31:18 -0500 Subject: [PATCH 4/6] for loop 1-1000 --- 04week/loop.js | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/04week/loop.js b/04week/loop.js index 4dfbea15b..2e50d7ecb 100644 --- a/04week/loop.js +++ b/04week/loop.js @@ -42,3 +42,13 @@ for (let item in persons) { console.log(persons.birthDate); } } + +// Use a for loop to console.log the numbers 1 to 1000. + +// Create a variable equal to an empty string. This will be used to pass info into. +const num = [""]; + +// Start at 0 and loop through the array 1000 times, adding 1 to the value each time. +for (let x = 0; x < 1001; x++) { + console.log(x); +} From 080d6d0f42a1a95243bed1872a2a01f6fafbfe1b Mon Sep 17 00:00:00 2001 From: Abby Gottlich Date: Sat, 3 Nov 2018 11:32:20 -0500 Subject: [PATCH 5/6] do...while loop 1-1000 --- 04week/loop.js | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) diff --git a/04week/loop.js b/04week/loop.js index 2e50d7ecb..32c4124cb 100644 --- a/04week/loop.js +++ b/04week/loop.js @@ -52,3 +52,19 @@ const num = [""]; for (let x = 0; x < 1001; x++) { console.log(x); } + +// Use a do...while loop to console.log the numbers from 1 to 1000. + +// Set a variable equal to an empty string to pass info to later. +let nums = ""; + +// Set another variable equal to 0 as your starting point. +let i = 0; + +// As long as your starting variable is less than 1000, add 1 and an empty space to the value. +do { + i = i + 1; + nums = nums + i + " "; +} while (i < 1000); + +console.log(nums); From a5b57bf47e61256bc49a7f22a4cf73b691f59e24 Mon Sep 17 00:00:00 2001 From: Abby Gottlich Date: Sat, 3 Nov 2018 11:34:17 -0500 Subject: [PATCH 6/6] loop questions and answers --- 04week/loop.js | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+) diff --git a/04week/loop.js b/04week/loop.js index 32c4124cb..a25b94f8e 100644 --- a/04week/loop.js +++ b/04week/loop.js @@ -68,3 +68,23 @@ do { } while (i < 1000); console.log(nums); + +// When is a for loop better than a while loop? +console.log( + "A for loop is better than a while loop when you know you want your code to run a certain number of times." +); + +// How is the readability of the code affected? +console.log( + "For loops are less readable because they are not similar to the way you would read it in English. While loops have better readability because it's closer to how we would read it in English." +); + +// What is the difference between a for loop and a for...in loop? +console.log( + "A for loop runs through the entire array or object each time it iterates, and has a specified starting and ending point. A for...in loop runs code on each individual item within the array or object each time it iterates and ends once it has run the specific code on each of those items." +); + +// What is the difference between a while loop and a do...while loop? +console.log( + "A do...while loop runs through the code at least once before checking whether or not the condition is true or false. The while loop always checks the condition first and will not run the code unless it is true." +);