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 example = [1,2,5,7,9,11,13,17,19] | |
const example2 = [-2,5,8,14,1,5,-9,-10,2] | |
avgWithReduce = function(arr) { | |
return arr.reduce(function(acc, number) { | |
// add accumulator to average of each number | |
return acc + number/arr.length | |
// set initial value to 0 | |
}, 0) | |
} |
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
// An simple zip function with reduce() | |
const example = [[1,2,3], [4,5,6], [7,8,9]] | |
const zip = arr => { | |
//loop through outter array | |
return arr.reduce(function(outter, inner) { | |
// loop through inner arrays | |
inner.forEach(function(num, i) { | |
// check if there is an array to add zipped values to |
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
function getResults(a, b) { | |
var square1 = a * a; | |
var double2 = b * 2; | |
var sum = square1 + double2; | |
var answer = sum - (a * 2); | |
if (answer % 2 === 0) { // is even | |
answer *= 5; | |
} else { | |
answer *= 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 getCleanerResults = (a,b) => { | |
return checkPrime(add101(multiply5or10(subtract(sum(square(a), double(b)), a)))) | |
} | |
const square = x => x**2 | |
const double = x => x * 2 | |
const sum = (a,b) => a + b | |
const subtract = (a,b) => a - b * 2 | |
const multiply5or10 = x => (x % 2 === 0) ? x * 5 : x * 10 | |
const add101 = x => x + 101 |
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
// deep clone | |
const deepClone = obj => { | |
// check to see if argument is an array/object and assign it to new variable | |
const newObj = (Array.isArray(obj) | |
? [] | |
: {}) | |
// loop through keys of object OR indexes of array | |
// Object.entries() work for arrays! | |
for (const [k, v] of Object.entries(obj)) { | |
// start deep clone |
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
let ex = ['a', 'b', 'c'] | |
let ex1 = [['a','b'], [['c']]] | |
let ex2 = { | |
a: 1, | |
b: 2, | |
c: 3 | |
} | |
let ex3 = { | |
a: [1,2,3, {g: 6, h: 7}], | |
b: { |
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 object1 = {name: 'Tu', hobbies: 'coding'} | |
copyAndCreateNewObject = (prevObj, key, value) => { | |
const obj = {} | |
for (let k in prevObj) { | |
obj[k] = prevObj[k] | |
} | |
obj[key] = value | |
return obj | |
} |
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 object1 = {name: 'Tu', hobbies: 'coding'} | |
createNewObjectWithES6 = (prevObj, key, value) => { | |
return { | |
...prevObj, | |
[key]: value | |
} | |
} | |
console.log(createNewObjectWithES6(object1, 'age', 31)) | |
// { name: 'Tu', hobbies: 'coding', age: 31 } |
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 addWithLoops = x => { | |
// initializes variable sum to 0 | |
let sum = 0 | |
// loop through all numbers 0 to 100 | |
for (let i = 0; i <= x; i++) { | |
// checks each number if is divisible by 2 | |
if (i % 2 === 0) { | |
// adds number to variable sum if it is | |
sum += i | |
} |
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 addWithRecursion = x => { | |
// base case | |
return x <= 0 | |
// ends function when x hits 0 | |
? 0 | |
// checks if x divides evenly by 2 | |
: x % 2 === 0 | |
// retains x and adds it to the function call of x - 1 | |
? x + addWithRecursion(x - 1) | |
// ignores x and moves on to the function call of x - 1 |
OlderNewer