-
Notifications
You must be signed in to change notification settings - Fork 0
Class07 loops #9
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: gh-pages
Are you sure you want to change the base?
Changes from all commits
38c8153
58ffcaf
f8b7c19
1971376
fb6c7e1
518fd85
bb8c037
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,68 @@ | ||
| 'use strict'; | ||
|
|
||
| // for loop: | ||
| // 1. Use a for loop to console.log each item in the array carsInReverse. | ||
| // for...in loop: | ||
| // 2. 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" | ||
| // 3. Use a for...in loop to console.log each key. | ||
| // 4. Then use a for...in loop and if state to console.log the value associated with the key birthDate. | ||
| // while loop: | ||
| // 5. Use a for loop to console.log the numbers 1 to 1000. | ||
| // do...while loop: | ||
| // 6. Use a do...while loop to console.log the numbers from 1 to 1000. | ||
|
|
||
|
|
||
| // 1. | ||
| const carsInReverse = ["mercedes", "bmw", "corvette", "ferrari"]; | ||
|
|
||
| for (i = 0; i < carsInReverse.length; i++) { | ||
| console.log(carsInReverse[i]); | ||
| } | ||
|
|
||
| // 2. | ||
| const persons = { | ||
| firstName: "Jane", | ||
| lastName: "Doe", | ||
| birthDate: "Jan 5, 1925", | ||
| gender: "female" | ||
| }; | ||
|
|
||
| // 3. | ||
| for (const keys in persons) { | ||
| console.log(Object.keys(persons)); | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. this would console.log every key in the persons array, for every key. You should just console.log(key) |
||
| } | ||
|
|
||
| // 4. | ||
| for (const keys in persons) { | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. should be |
||
| if (keys === 'birthDate') { | ||
| console.log(persons[keys]); | ||
| } | ||
| } | ||
|
|
||
| // 5 | ||
| let num = 1; | ||
| while(num <= 1000) { | ||
| console.log(num); | ||
| num++; | ||
| } | ||
|
|
||
| // 6 | ||
| let num = 1; | ||
| do { | ||
| console.log(num); | ||
| num++; | ||
| } | ||
| while(num <= 1000); | ||
|
|
||
| // 1. When is a for loop better than a while loop? | ||
| // A for loop is better than a while loop when you know how many iterations there will be. | ||
| // 2. How is the readability of the code affected | ||
| The conditions in a for loop are clearly defined in the statement where as the while loop has less required conditions and therefor is easier to read and understand standalone, but is also more prone to errors. | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. this should be commented out |
||
| // 3. What is the difference between a for loop and a for...in loop? | ||
| // A for...in loop is a for loop specified to iterate over the enumerable properties of an object. | ||
| // 4. What is the difference between a while loop and a do...while loop? | ||
| // A do...while loop will repeat until a specific condition evaluates to false (which is defined after the statement) and a while loop will repeat as long as the condition (defined in the statement) evaluates to true. | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
should be
key