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
78 changes: 78 additions & 0 deletions 04week/loop.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
'use strict';

const cars = ["honda","ford","fonda","hoard","horse"];

const carsInReverse = (arr1) => {
arr1.reverse().forEach(car => {
console.log(car)
});
}

carsInReverse(cars);

cars.forEach(car => {
console.log(car)
});

const persons = {
firstName: "Jane",
lastName: "Doe",
birthDate: "Jan 5, 1925",
gender: "female",
}

const printPersonalDataKeys = (obj1) => {

Choose a reason for hiding this comment

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

great use of using a function that uses a obj for a parameter!!!

for (const personalData in obj1){
console.log(personalData);
}
}

printPersonalDataKeys(persons);

const printPersonBirthday = (obj1) => {
for (const personalData in obj1){
if(personalData=="birthDate") return console.log(obj1[personalData]);
}
}

printPersonBirthday(persons);

const countForLoop = ()=>{
for(let i = 1; i<1001;i++){
console.log(i)
}
}

const countWhileLoop = () => {
let counter = 1;
while(counter<1001){
console.log(counter);
counter++;
}
}

const countDoWhileLoop = () => {
let counter = 1;
do {
console.log(counter);
counter++;
} while (counter<1001)
}

// When is a for loop better than a while loop?
// for loops are good for a known number of iterations for loops are generally better

// How is the readability of the code affected?
//

// What is the difference between a for loop and a for...in loop?
// A for loop is a general case counting loops where as for in is a loop specifically for objects


// What is the difference between a while loop and a do...while loop?
// a while loop runs as long as a condution is true, a while loop will run at least once, and while the condition is true


Choose a reason for hiding this comment

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

Just need these answered:

When is a for loop better than a while loop?
How is the readability of the code affected?
What is the difference between a for loop and a for...in loop?
What is the difference between a while loop and a do...while loop?

countForLoop();
countWhileLoop();
countDoWhileLoop();