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
47 changes: 47 additions & 0 deletions 04week/loop.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
'use strick'
const cars = ['ford', 'audi', 'bmw', 'volvo', 'tata', 'chevrolet', 'tesla'];
for (i = 0; i <= cars.length; i++) {
console.log(cars[i]);
}

var person = {
firstName: "Jane",
lastName: "Doe",
birthDate: "Jan 5, 1925",
gender: "female"
};


for (const keys in person){
console.log(keys);
}


for (const keys in person){
if (keys === 'birthDate'){
console.log(person[keys]);
}
}


let numArray = [];
for ( i = 1; i <= 1000; i++) {
Copy link

Choose a reason for hiding this comment

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

This should be a while loop, not a for loop.

numArray.push(i);
}
console.log(numArray);


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


// The for loop would be used if the number of iterations is known. The while loop would be better suited if the loop has an uncertain number of iterations or dependant on a particular condition.

//Overall the readabilty of the For loop is better.

// for loops are meant to iterate while incrementing/decrementing. For..in loops are meant to enumerate through object properties.

//A while loop in javascript loops through a block of code while a condition is true, a do/while loops through a block of code once, and then repeats the loop while a specified condition is true.