For Loops Flashcards

1
Q

for (let x in y):

A

let obj = {a: 1, b: 2, c: 3};

for (let key in obj) {
console.log(key + “: “ + obj[key]);
}

// Output:
// a: 1
// b: 2
// c: 3

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

while loop
finding the largest element in the array

A

let arr = [3, 7, 2, 9, 4, 6];
let max = arr[0];
let i = 1;

while (i < arr.length) {
if (arr[i] > max) {
max = arr[i];
}
i++;
}

console.log(“The largest element is:”, max);

How well did you know this?
1
Not at all
2
3
4
5
Perfectly