"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'
A

emotion

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
Q
var arr = [[1,2], [3,4], [5,6]];
WRITE NESTED FOR loop using FOR
    console.log(arr[i][j]);
  }
}
A
for (var i=0; i < arr.length; i++) {
  for (var j=0; j < arr[i].length; j++) {
26
Q
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
A

console.log(arr[i][j]);

27
Q
var cubes = [
 [1, 2, 3],
 [4, 5, 6],    
 [7, 8, 9],
];

“cube[0][0] = 1”

WRITE NESTED FOR LOop

A
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
Q

var arr = [[1,2], [3,4], [5,6]];

//write NESTED for loop i j

console.log(arr[i][j]);   } }
A
for (var i=0; i < arr.length; i++) {
  for (var j=0; j < arr[i].length; j++) {
29
Q

var arr = [[1,2], [3,4],[5,6]];

write FOR IN

A
for (i in arr){
      for (j in arr[i]){
        console.log(arr[i][j]);
      }
    }
30
Q

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 = [];
A
const dayAbbreviations = days.forEach(day.slice(0,2))
dayAbbreviations.push(dayAbbreviations)
31
Q

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

A

numbers.forEach(function(number){
times10.push(number * 10)
});

32
Q

MAKE TO ARROW FUNC

numbers.forEach(function(number){
times10.push(number * 10)
});

A

numbers.forEach(number => times10.push(number * 10));

33
Q

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.

A

.map()

.reduce()

.forEach() i

34
Q

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 _______ , __________ & ____________

A

Arrays, Maps, and Sets.

35
Q

WRITE AS A FOREACH

const arr = ['cat', 'dog', 'fish'];
for (i = 0; i < arr.length; i++) { 
  console.log(arr[i])
}
A

arr.forEach(element => {
console.log(element);
});

36
Q
const obj = {  
  a: 1,
  b: 2,
  c: 3,
  d: 4
}

for (let elem in obj) {
console.log( obj[elem] )
}

A

1 , 2, 3, 4

37
Q
for (let elem in obj) {
  console.log(\_\_\_\_\_\_\_\_\_\_\_\_\_\_);
}
// a = 1
// b = 2
// c = 3
// d = 4
A

${elem} = ${obj[elem]}

38
Q
const arr = ['cat', 'dog', 'fish'];
for (let i in arr) {  
  \_\_\_\_\_\_\_\_\_\_\_\_
}
// cat
// dog
// fish
A

console.log(arr[i])

39
Q

const string = ‘hello’;

//write a FOR…IN

// h
// e
// l
// l
// o
A

for (let character in string) {
console.log(string[character])
}

40
Q

A drawback to using forEach is it cannot use

A

break

41
Q
var sandwiches = [
	'tuna',
	'ham',
	'turkey',
	'pb&amp;j'
];

sandwiches.forEach(function (sandwich, index) {
console.log(index);
console.log(sandwich);
});

// returns

A

0, “tuna”, 1, “ham”, 2, “turkey”, 3, “pb&j”

42
Q
var sandwiches = [
	'tuna',
	'ham',
	'turkey',
	'pb&amp;j'
];

//code using forEach

// returns 0, “tuna”, 1, “ham”, 2, “turkey”, 3, “pb&j”

A

sandwiches.forEach(function (sandwich, index) {
console.log(index);
console.log(sandwich);
});

43
Q
arr.forEach(function callback(\_\_\_\_\_\_\_ , \_\_\_\_\_\_\_, \_\_\_\_\_\_) {
    //your iterator
}
A

current value, index, array

44
Q
var words = ['one', 'two', 'three', 'four'];
words.forEach(function(word) {
  console.log(word);
  if (word === 'two') {
    words.shift();
  }
});
A
// one
// two
// four
45
Q

WRITE AS A FOR

const fruits = [‘apple’, ‘pear’, ‘cherry’];

// logs apple, pear, cherry

A

for (i = 0; i < fruits.length; i++) {
console.log(fruits[i])
}

46
Q

WRITE AS A WHILE

const fruits = [‘apple’, ‘pear’, ‘cherry’];

A
var counter = 1;
while (counter < 10 ) {
  console.log( counter );
  counter = counter + 1;
}
47
Q

WRITE AS A DO WHILE

const fruits = [‘apple’, ‘pear’, ‘cherry’];

A
var counter = 1;
do {
  counter = counter + 1;
  console.log( counter );
} while (counter < 10);
48
Q

WRITE AS A MAP

const fruits = [‘apple’, ‘pear’, ‘cherry’];

console.log(capitalizedFruits) // [ ‘APPLE’, ‘PEAR’, ‘CHERRY’ ]

A

const capitalizedFruits = fruits.map( fruit => fruit.toUpperCase() );

49
Q

Nesting _______ is really a bad practice. Instead of that you can use the map() function to get data.

A

foreach

50
Q

it iterate through a nested array its best to use _____ or ______

A

for

map

51
Q
var array1 = ['a', 'b', 'c'];
var iterator = array1.keys(); 

write a FOR OF loop

// expected output: 0 1 2

A

for (letkey of iterator) {
console.log(key);
}