Skip to content

Instantly share code, notes, and snippets.

View katepapineni's full-sized avatar
🚀
Happy to be here!

Kate katepapineni

🚀
Happy to be here!
View GitHub Profile
const rFunc = n => (n === 0 ? 1 : n * rFunc(n - 1));
// What's the ouput? What's the base case?
console.log(rFunc(4));
/* || 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
);
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));
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;
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));
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];
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);
/* 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
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'
);
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(' + '));