"For" deck Flashcards
for..in used with Objects?
Yes
for..in used with Arrays?
Yes, not advised
for..in used with Strings
Yes, not advised
for..of used with Objects?
No
for..of used with Arrays?
Yes
for..of used with Strings
Yes
const obj = { a: 1, b: 2, c: 3, d: 4 }
WRITE A FOR..IN LOOP
// Result: 1, 2, 3, 4
for (var property1 in object1) {
console.log(object1[property1]);
}
Write a blank for in loop
for (variable in enumerable) { // do stuff }
const array = [‘a’, ‘b’, ‘c’, ‘d’];
Write a for loop
// Result: a, b, c, d
for (let i = 0; i < array.length; i++) { console.log(array[i]); }
The for..of loop doesn’t work with Objects because they are not ________ and therefore don’t have a [Symbol.iterator] property.
“iterable”,
The for..of loop doesn’t work with_________ because they are not “iterable”, and therefore don’t have a [Symbol.iterator] property.
Objects
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.
Arrays
Strings
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
for..in
let iterable = [10, 20, 30];
write for..of
// 10 // 20 // 30
for (const value of iterable) {
console.log(value);
}
let iterable = ‘boo’;
write for..of
// "b" // "o" // "o"
for (let value of iterable) {
console.log(value);
}
var string1 = ""; var object1 = {a: 1, b: 2, c: 3};
write a for in
console.log(string1); // expected output: "123"
for (var property1 in object1) {
string1 += object1[property1];
}
__________ should not be used to iterate over an Array where the index order is important.
for…in
for…in should not be used to iterate over an_______ where the index order is important.
Array
var fruits =[“apple”, “pear”, “plum”];
use for Each to interate in console.
fruits.forEach(fruit => console.log(fruit));
the for Each loop cannot _________ of loops like the for loop
break
WRITE A REVERSE STRING for string using FOR
function reverseString(str) {
}
reverseString(‘hello’);
var newString = ""; for (var i = str.length - 1; i >= 0; i--) { newString += str[i]; } return newString;
const emotions = ['happy', 'sad', 'angry']; emotions.forEach( emotion => console.log(\_\_\_\_\_\_\_\_\_) );
WHAT GOES IN _______
// Will log the following: // 'happy' // 'sad' // 'angry'
emotion
const emotions = ['happy', 'sad', 'angry']; emotions.forEach( \_\_\_\_\_\_\_\_\_\_\_\_\_\_ ); // Will log the following: // 'happy' // 'sad' // 'angry'
emotion => console.log(emotion)
function reverseString(str) { var newString = ""; for (var i = str.length - 1; i >= 0; i--) { \_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_ } console.log (newString); } reverseString('hello');
newString += str[i];
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++) {
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]);
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]); } }
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++) {
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]); } }
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)
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)
});
MAKE TO ARROW FUNC
numbers.forEach(function(number){
times10.push(number * 10)
});
numbers.forEach(number => times10.push(number * 10));
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
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.
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);
});
const obj = { a: 1, b: 2, c: 3, d: 4 }
for (let elem in obj) {
console.log( obj[elem] )
}
1 , 2, 3, 4
for (let elem in obj) { console.log(\_\_\_\_\_\_\_\_\_\_\_\_\_\_); } // a = 1 // b = 2 // c = 3 // d = 4
${elem} = ${obj[elem]}
const arr = ['cat', 'dog', 'fish']; for (let i in arr) { \_\_\_\_\_\_\_\_\_\_\_\_ } // cat // dog // fish
console.log(arr[i])
const string = ‘hello’;
//write a FOR…IN
// h // e // l // l // o
for (let character in string) {
console.log(string[character])
}
A drawback to using forEach is it cannot use
break
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”
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);
});
arr.forEach(function callback(\_\_\_\_\_\_\_ , \_\_\_\_\_\_\_, \_\_\_\_\_\_) { //your iterator }
current value, index, array
var words = ['one', 'two', 'three', 'four']; words.forEach(function(word) { console.log(word); if (word === 'two') { words.shift(); } });
// one // two // four
WRITE AS A FOR
const fruits = [‘apple’, ‘pear’, ‘cherry’];
// logs apple, pear, cherry
for (i = 0; i < fruits.length; i++) {
console.log(fruits[i])
}
WRITE AS A WHILE
const fruits = [‘apple’, ‘pear’, ‘cherry’];
var counter = 1; while (counter < 10 ) { console.log( counter ); counter = counter + 1; }
WRITE AS A DO WHILE
const fruits = [‘apple’, ‘pear’, ‘cherry’];
var counter = 1; do { counter = counter + 1; console.log( counter ); } while (counter < 10);
WRITE AS A MAP
const fruits = [‘apple’, ‘pear’, ‘cherry’];
console.log(capitalizedFruits) // [ ‘APPLE’, ‘PEAR’, ‘CHERRY’ ]
const capitalizedFruits = fruits.map( fruit => fruit.toUpperCase() );
Nesting _______ is really a bad practice. Instead of that you can use the map() function to get data.
foreach
it iterate through a nested array its best to use _____ or ______
for
map
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);
}