This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const rFunc = n => (n === 0 ? 1 : n * rFunc(n - 1)); | |
// What's the ouput? What's the base case? | |
console.log(rFunc(4)); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* || operator vs. If */ | |
const hero = { name: 'Wonder Woman', age: 0, address: null }; | |
// As || operator | |
const getOrOperator = ({ name, age, address }) => ( | |
address // hero.address is falsy | |
|| age // hero.age is falsy | |
|| name // hero.name is truthy so short-circuits and is returned | |
|| 'hero' // default | |
); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const min = 4; | |
const max = 9; | |
const getNum = num => Math.min(max, Math.max(min, num)); | |
// What's the output? | |
console.log(getNum(6)); | |
console.log(getNum(3)); | |
console.log(getNum(10)); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const getRange = (start, end) => { | |
const range = []; | |
const date = new Date(start); | |
const toDate = new Date(end); | |
while (date <= toDate) { | |
range.push(new Date(date)); | |
date.setDate(date.getDate() + 1); | |
} | |
return range; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const numbers = [3, 11, 29, 19, 7]; | |
const sort1 = (a, b) => a - b; | |
const sort2 = (a, b) => b - a; | |
// What's the output ? | |
console.log(numbers.sort(sort1)); | |
console.log(numbers.sort(sort2)); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const cities = ['Sydney', 'Rome', 'Tokyo', 'Delhi', 'Zurich', ]; | |
const getCity = (city) => { | |
return cities.splice(city, 2); | |
} | |
const city1 = getCity(cities.indexOf('Seattle')); | |
cities.length = 5; | |
const city2 = cities[cities.length - 1]; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const flights = [ | |
{ route: 'Frankfurt to Hong Kong', hours: 11.5 }, | |
{ route: 'San Fransisco to Delhi', hours: 15.75 }, | |
{ route: 'Singapore to Paris', hours: 13.5 }, | |
{ route: 'Delhi to Dubai', hours: 4 }, | |
]; | |
const findFlights = (city) => { | |
return (flight) => { | |
return flight.route.includes(city); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* Ternary vs. If-Else */ | |
const min = 2; | |
const max = 7; | |
// As Ternary operation | |
const getNumTernary = (num) => { | |
return num < min ? min : num > max ? max : num; | |
} | |
// As If - Else statements |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const hero1 = { name: 'Wonder Woman', age: 0, address: null, }; | |
const hero2 = { name: 'Thor', age: 1500, address: '', }; | |
const getInfo = ({ name, age, address }) => ( | |
address | |
|| age | |
|| name | |
|| 'hero' | |
); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const min = 2; | |
const max = 7; | |
const getNum = (num) => { | |
return num < min ? min : num > max ? max : num; | |
} | |
// What's the output? | |
console.log([getNum(9), getNum(1), getNum(4)].join(' + ')); |
NewerOlder