Thursday Flashcards
(4 cards)
difference between the postfix and the prefix increment operator
let num = 5;
let result = num++;
console.log(num); // Output: 6 (num was incremented to 6)
console.log(result); // Output: 5 (result holds the initial value of num before the increment)
VERSUS
prefix increment operator —- ++i
the value of the variable is incremented first, and then the updated value is used in the expression or statement
let num = 5;
let result = ++num;
console.log(num); // Output: 6 (num was incremented to 6)
console.log(result); // Output: 6 (result holds the value of the updated num)
https://www.udemy.com/course/the-web-developer-bootcamp/learn/lecture/37338930#overview
describe the logic behind using the OR operator to assign value… rewatch reduce video in Colt Steele’s udemy course
the OR operator looks for truthiness in the first statement (an undefined value is falsy & an actual bit of data like a num, string, whatever is truthy) — if that first value is empty, and the second value is truthy, the truth value will get returned
let someInput = “”
const name = someInput || ‘Max’
console.log(name)
const votes = [‘y’, ‘y’, ‘n’, ‘e’, ‘y’, ‘n’, ‘n’, ‘n’, ‘y’];
const tally = votes.reduce((tally, vote) => {
tally[vote] = 1 + (tally[vote] || 0);
return tally;
}, {});
console.log(tally)
map
set
Map objects are similar to object literals, but the key types can be any value and they’re actual iterables where you can determine the size of the map.
Sets are a collection of unique values
// Create a Set
const letters = new Set([“a”,”b”,”c”]);
//Convert to an array
Array.from or spread operator
const letters = [“a”,”b”,”c”];
const number = [1, 2, 3, 4];
two ways to convert a set to an array →
const combo1 = […new Set([…letters, …number])];
console.log(combo1); // [‘a’, ‘b’, ‘c’, 1, 2, 3, 4]
const combo2 = Array.from(new Set([…letters, …number]));
console.log(combo2); // [‘a’, ‘b’, ‘c’, 1, 2, 3, 4]
helpful articles :
https://www.w3schools.com/js/js_object_sets.asp
https://www.javascripttutorial.net/es6/javascript-set/
how would you average the values in this array using reduce?
const euros = [6, 10, 2];
const avg = euros.reduce((total, currVal) => total += currVal) / euros.length;
console.log(avg);
const sum = euros.reduce((total, currVal) => total += currVal, 0);
let avg = sum / euros.length
console.log(avg);