"For" deck Flashcards

1
Q

for..in used with Objects?

A

Yes

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

for..in used with Arrays?

A

Yes, not advised

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

for..in used with Strings

A

Yes, not advised

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

for..of used with Objects?

A

No

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

for..of used with Arrays?

A

Yes

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

for..of used with Strings

A

Yes

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q
const obj = {
	a: 1,
	b: 2,
	c: 3,
	d: 4
}

WRITE A FOR..IN LOOP

// Result: 1, 2, 3, 4

A

for (var property1 in object1) {
console.log(object1[property1]);
}

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

Write a blank for in loop

A
for (variable in enumerable) {
	// do stuff
}
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

const array = [‘a’, ‘b’, ‘c’, ‘d’];

Write a for loop

// Result: a, b, c, d

A
for (let i = 0; i < array.length; i++) {
  console.log(array[i]);
}
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

The for..of loop doesn’t work with Objects because they are not ________ and therefore don’t have a [Symbol.iterator] property.

A

“iterable”,

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

The for..of loop doesn’t work with_________ because they are not “iterable”, and therefore don’t have a [Symbol.iterator] property.

A

Objects

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

The for..of loop works well with________ and_________, as they are iterable. This method is a more reliable way of looping through an Array in sequence.

A

Arrays

Strings

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

it is generally advised that _________not be used with Arrays, particularly because it cannot be guaranteed that the iteration happens in sequence, which is usually important for Arrays

A

for..in

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

let iterable = [10, 20, 30];

write for..of

// 10
// 20
// 30
A

for (const value of iterable) {
console.log(value);
}

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

let iterable = ‘boo’;

write for..of

// "b"
// "o"
// "o"
A

for (let value of iterable) {
console.log(value);
}

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q
var string1 = "";
var object1 = {a: 1, b: 2, c: 3};

write a for in

console.log(string1);
// expected output: "123"
A

for (var property1 in object1) {
string1 += object1[property1];
}

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

__________ should not be used to iterate over an Array where the index order is important.

A

for…in

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

for…in should not be used to iterate over an_______ where the index order is important.

A

Array

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

var fruits =[“apple”, “pear”, “plum”];

use for Each to interate in console.

A

fruits.forEach(fruit => console.log(fruit));

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

the for Each loop cannot _________ of loops like the for loop

A

break

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

WRITE A REVERSE STRING for string using FOR

function reverseString(str) {

}
reverseString(‘hello’);

A
var newString = "";
    for (var i = str.length - 1; i >= 0; i--) {
        newString += str[i];
    }
    return newString;
22
Q
const emotions = ['happy', 'sad', 'angry'];
emotions.forEach( emotion => console.log(\_\_\_\_\_\_\_\_\_) );

WHAT GOES IN _______

// Will log the following:
// 'happy'
// 'sad'
// 'angry'
23
Q
const emotions = ['happy', 'sad', 'angry'];
emotions.forEach( \_\_\_\_\_\_\_\_\_\_\_\_\_\_ );
// Will log the following:
// 'happy'
// 'sad'
// 'angry'
A

emotion => console.log(emotion)

24
Q
function reverseString(str) {
    var newString = "";
    for (var i = str.length - 1; i >= 0; i--) {
        \_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_ 
    }
    console.log (newString);
}
reverseString('hello');
A

newString += str[i];

25
``` var arr = [[1,2], [3,4], [5,6]]; WRITE NESTED FOR loop using FOR console.log(arr[i][j]); } } ```
``` for (var i=0; i < arr.length; i++) { for (var j=0; j < arr[i].length; j++) { ```
26
``` var arr = [[1,2], [3,4], [5,6]]; for (var i=0; i < arr.length; i++) { for (var j=0; j < arr[i].length; j++) { _____________ print to console } } 1,2,3,4,5,6 ```
console.log(arr[i][j]);
27
``` var cubes = [ [1, 2, 3], [4, 5, 6], [7, 8, 9], ]; ``` "cube[0][0] = 1" WRITE NESTED FOR LOop
``` for(var i = 0; i < cubes.length; i++) { var cube = cubes[i]; for(var j = 0; j < cube.length; j++) { display("cube[" + i + "][" + j + "] = " + cube[j]); } } ```
28
var arr = [[1,2], [3,4], [5,6]]; //write NESTED for loop i j console.log(arr[i][j]); } }
``` for (var i=0; i < arr.length; i++) { for (var j=0; j < arr[i].length; j++) { ```
29
var arr = [[1,2], [3,4],[5,6]]; write FOR IN
``` for (i in arr){ for (j in arr[i]){ console.log(arr[i][j]); } } ```
30
Using forEach, copy only the first 2 characters of each string in the days array and store the abbreviations in the dayAbbreviations array. ``` const days = ['Sunday', 'Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday']; let dayAbbreviations = []; ```
``` const dayAbbreviations = days.forEach(day.slice(0,2)) dayAbbreviations.push(dayAbbreviations) ```
31
Using forEach, iterate over the numbers array and multiply each number by 10, storing these new numbers in the times10 array. ``` const numbers = [1,2,3,4,5,6,7,8,9,10]; let times10 = []; ``` USE PUSH
numbers.forEach(function(number){ times10.push(number * 10) });
32
MAKE TO ARROW FUNC numbers.forEach(function(number){ times10.push(number * 10) });
numbers.forEach(number => times10.push(number * 10));
33
You should favor _______and .__________ if you prefer the functional paradigm of programming. For other paradigms (and even in some rare cases within the functional paradigm),_________ is the proper choice.
.map() .reduce() .forEach() i
34
ForEach forEach is an Array method that we can use to execute a function on each element in an array. It can only be used on _______ , __________ & ____________
Arrays, Maps, and Sets.
35
WRITE AS A FOREACH ``` const arr = ['cat', 'dog', 'fish']; for (i = 0; i < arr.length; i++) { console.log(arr[i]) } ```
arr.forEach(element => { console.log(element); });
36
``` const obj = { a: 1, b: 2, c: 3, d: 4 } ``` for (let elem in obj) { console.log( obj[elem] ) }
1 , 2, 3, 4
37
``` for (let elem in obj) { console.log(______________); } // a = 1 // b = 2 // c = 3 // d = 4 ```
`${elem} = ${obj[elem]}`
38
``` const arr = ['cat', 'dog', 'fish']; for (let i in arr) { ____________ } // cat // dog // fish ```
console.log(arr[i])
39
const string = 'hello'; //write a FOR...IN ``` // h // e // l // l // o ```
for (let character in string) { console.log(string[character]) }
40
A drawback to using forEach is it cannot use
break
41
``` var sandwiches = [ 'tuna', 'ham', 'turkey', 'pb&j' ]; ``` sandwiches.forEach(function (sandwich, index) { console.log(index); console.log(sandwich); }); // returns
0, "tuna", 1, "ham", 2, "turkey", 3, "pb&j"
42
``` var sandwiches = [ 'tuna', 'ham', 'turkey', 'pb&j' ]; ``` //code using forEach // returns 0, "tuna", 1, "ham", 2, "turkey", 3, "pb&j"
sandwiches.forEach(function (sandwich, index) { console.log(index); console.log(sandwich); });
43
``` arr.forEach(function callback(_______ , _______, ______) { //your iterator } ```
current value, index, array
44
``` var words = ['one', 'two', 'three', 'four']; words.forEach(function(word) { console.log(word); if (word === 'two') { words.shift(); } }); ```
``` // one // two // four ```
45
WRITE AS A FOR const fruits = ['apple', 'pear', 'cherry']; // logs apple, pear, cherry
for (i = 0; i < fruits.length; i++) { console.log(fruits[i]) }
46
WRITE AS A WHILE const fruits = ['apple', 'pear', 'cherry'];
``` var counter = 1; while (counter < 10 ) { console.log( counter ); counter = counter + 1; } ```
47
WRITE AS A DO WHILE const fruits = ['apple', 'pear', 'cherry'];
``` var counter = 1; do { counter = counter + 1; console.log( counter ); } while (counter < 10); ```
48
WRITE AS A MAP const fruits = ['apple', 'pear', 'cherry']; console.log(capitalizedFruits) // [ 'APPLE', 'PEAR', 'CHERRY' ]
const capitalizedFruits = fruits.map( fruit => fruit.toUpperCase() );
49
Nesting _______ is really a bad practice. Instead of that you can use the map() function to get data.
foreach
50
it iterate through a nested array its best to use _____ or ______
for | map
51
``` var array1 = ['a', 'b', 'c']; var iterator = array1.keys(); ``` write a FOR OF loop // expected output: 0 1 2
for (letkey of iterator) { console.log(key); }