Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
56 changes: 56 additions & 0 deletions 04week/loop.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
const carsInReverse = ["Ford", "Saab", "Volvo", "BWM", "Toyota", "Honda"];
for (x = 0; x < carsInReverse.length; x ++) {

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

x is not defined.

console.log(carsInReverse[x]);
};


const persons = {
firstName: "Jane",
lastName: "Doe",
birthDate: "Jan 5, 1925",
gender: "female",
}
for (key in persons) {

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

key is not defined.

console.log(key);
}
for (key in persons) {

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

key is not defined.

if (key == "birthDate") {
console.log(persons.birthDate)
}
};


let x = 0;
while (x < 1000) {
x++;
console.log(x);
}


let y = 0;
do {
y++;
console.log(y);
} while (y < 1000);


// When is a for loop better than a while loop?

//for loop is better when you want to run code a certain number of times,
//while is better when you want to keep running that code until the condition.


// How is the readability of the code affected?

//I think for loops are more concise, and your variable is set within the loop,
//in while loops looks like you have to set the variable beforehand.


// What is the difference between a for loop and a for...in loop?

//for in loops are better suited for objects


// What is the difference between a while loop and a do...while loop?

//in while loops, test expression is checked at first but, in do...while loops code is executed at first then the condition is checked