Friday Flashcards
(6 cards)
how to destructure objects or arrays
https://www.udemy.com/course/the-web-developer-bootcamp/learn/lecture/22003578#overview
describe how to add / remove elements from the beginning or end of an array.
create a sample array to test these methods.
array methods (description / return val) :
some
every
sort
find
*shorter-named method removes
push method to add to end – arrayName.push(element to be added)
pop method to remove from end – arrayName.pop ( )
*shorter-named method removes
unshift method to add to beginning – arrayName.unshift(element to be added)
shift method to remove from end – arrayName.shift ( )
some : functionality : test if at least one element in the array passes the test; return : boolean value
every : functionality : test if every element in the array pass the callback test; return : boolean value
sort reference video :
https://www.udemy.com/course/javascript-beginners-complete-tutorial/learn/lecture/17010248#overview
https://coursework.vschool.io/array-sort-exercises/
part 2 of slide 2 :
WILL UPDATE, SKIP FOR NOW
what does array find & findIndex array methods do?
why would you use find or findIndex vs filter?
use find with to return the value with 5 letters, and
use findIndex to return the first value with a ‘u’ :
let cities = [“Ankara”, “Istanbul”, “Antalya”, “Bursa”, “Trabzon”];
find ( ) —- iterate through an array until you find an item that satisfies the callback & return THAT ITEM. you can also pass in a function for more complicated searches
SYNTAX : arrayName.find (callbackFn);
example w/o a function :
let cities = [“Ankara”, “Istanbul”, “Antalya”, “Bursa”, “Trabzon”];
let city = “Bursa”;
let chars = 7;
let match1 = cities.find(item => city === item ? true : null);
console.log(item from cities that matched was : ${match1}
);
// should return ‘item from cities that matched was : Bursa’
let match2 = cities.find(item => chars === item.length);
console.log(‘item with matching length at position’, match2);
// let match2 = cities.find(item => chars === item.length);
_______________________________________________________________
findIndex ( ) —- iterate through an array, look for a matching item & return the POSITION
SYNTAX : arrayName.find (callbackFn);
let cities = [“A’Tnkara”, “Istanbul”, “Antalya”, “Bursa”, “Trabzon”];
let index = cities.findIndex(item => item.toLowerCase().indexOf(“t”) > -1);
console.log(Letter 't' found in item at index ${index}
);
// ‘Letter ‘t’ found in item at index 0’
let word = cities.find(item => item.toLowerCase().indexOf(“t”) > -1);
console.log(word found : ${word}
);
//’Letter ‘T’ found in item at index AnkaTra’
/* / / / / / / / / / / / / / / */
filter is helpful for testing multiple values & returning an array, versus a single value
reference video : https://www.youtube.com/watch?v=9b6gxQZHvis
flatten the colors into one array using reduce, then remove duplicates using Set & EXPLAIN your syntax
flatten the colors into one array w/o duplicates using reduce / forEach, but NOT using Set
const data = [
{a: ‘happy’, b: ‘robin’, c: [‘blue’,’green’]},
{a: ‘tired’, b: ‘panther’, c: [‘green’,’black’,’orange’,’blue’]},
{a: ‘sad’, b: ‘goldfish’, c: [‘green’,’red’]}
];
const colors = data.reduce((total, amount) => {
amount.c.forEach( color => {
total.push(color);
})
return total;
}, []);
// edit : return […new Set(total)];
// […new Set] copies the values in the set into an array
}, [ ]);
CAN ALSO USE CONCAT
const flatten = data.reduce((total, currVal) => total.concat(currVal.c), []);
const flatten = data.reduce((total, currVal) => […new Set (total.concat(currVal.c))], []);
PT 2
const colors = data.reduce((total, currVal) => […new Set ([…total, …currVal.c])],
[ ],
);
// […new Set] copies the set into an array
// […total, …currVal.c] spreads the values paired with c in each object into the empty array, total
PT 3
const uniqueColors = data.reduce((total, amount) => {
amount.c.forEach( color => {
if (total.indexOf(color) === -1){
total.push(color);
}
});
return total;
}, []);
describe the structure of switch statements & its use case. also describe how you’d combine logic in a switch statement.
USE CASE :
you have one value and you’re checking it against multiple different conditions to determine output based on that changing value
argument for switch : the variable you’re comparing against
case : each case is a different potential value stored in variable
break : need a break statement after each case’s logic, otherwise all values following will be returned/logged
default : your “else” / catch-all
overlapping cases : combine logic by stacking cases and applying a break
________________________________
let emoji = “heart”;
// Switch with overlapping cases… in this case, with no code to run after ‘heart’ AND no break statement, it combines the next case with the current
switch (emoji) {
case “sad face”:
case “happy face”:
console.log(“yellow”);
break;
case “eggplant”:
console.log(“purple”);
break;
case “heart”:
case “lips”:
console.log(“red”);
break;
default:
console.log(“no emoji entered”);
}
// red
________________________________
reference video : https://www.udemy.com/course/javascript-beginners-complete-tutorial/learn/lecture/16777632#overview
mdn : https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/switch
promises / async await