diff --git a/06week/functionalJavaScript/01helloWorld.js b/06week/functionalJavaScript/01helloWorld.js new file mode 100644 index 000000000..730126165 --- /dev/null +++ b/06week/functionalJavaScript/01helloWorld.js @@ -0,0 +1,8 @@ +'use strict'; + +// Write a function that takes an input string and returns it uppercased. + +function upperCaser(input) { + return input.toUpperCase(); +} +module.exports = upperCaser diff --git a/06week/functionalJavaScript/02higherOrder.js b/06week/functionalJavaScript/02higherOrder.js new file mode 100644 index 000000000..50a168147 --- /dev/null +++ b/06week/functionalJavaScript/02higherOrder.js @@ -0,0 +1,17 @@ +'use strict'; + +// Implement a function that takes a function as its first argument, a number num as its second argument, +// then executes the passed in function num times. + +const repeat = (operation, num) => { + // if (num === 0) { + // return true; + // } + // operation(); + // return repeat(operation, num - 1); + for (i = 0; i < num; i++) { + operation(); + } +} + +module.exports = repeat diff --git a/06week/functionalJavaScript/03basicMap.js b/06week/functionalJavaScript/03basicMap.js new file mode 100644 index 000000000..d61613e9b --- /dev/null +++ b/06week/functionalJavaScript/03basicMap.js @@ -0,0 +1,26 @@ +'use strict'; + +// Convert the following code from a for-loop to Array#map: + +// function doubleAll(numbers) { +// var result = [] +// for (var i = 0; i < numbers.length; i++) { +// result.push(numbers[i] * 2) +// } +// return result +// } + +const doubleAll = (numbers) => { + // let result = []; + // numbers.map(i => result.push(i * 2)); + + return numbers.map(i => i * 2); + + // return result; + +} + +module.exports = doubleAll + + +// module.exports = numbers => numbers.map(i => i * 2); diff --git a/06week/functionalJavaScript/04basicFilter.js b/06week/functionalJavaScript/04basicFilter.js new file mode 100644 index 000000000..ae93a840c --- /dev/null +++ b/06week/functionalJavaScript/04basicFilter.js @@ -0,0 +1,33 @@ +'use script'; + +// Use Array#filter to write a function called getShortMessages. +// +// getShortMessages takes an array of objects with '.message' properties and returns an array of messages that are less than < 50 characters long. +// +// The function should return an array containing the messages themselves, without their containing object. + +// const getShortMessages = (messages) => { +// messages.filter(obj => obj.message.length < 50)) +// }; +// } + +// function getShortMessages(messages) { +// +// let shortMessage = messages.filter(function(shorten) { +// if (shorten.message.length < 50) { +// return true; +// } +// }); +// +// return shortMessage.map(function(shorten) { +// return shorten.message; +// }) +// } + +module.exports = getShortMessages + +const getShortMessages = (messages) => { + return messages.map(obj => obj.message).filter(msg => msg.length < 50); +} + +module.exports = getShortMessages diff --git a/06week/functionalJavaScript/05basicEverySome.js b/06week/functionalJavaScript/05basicEverySome.js new file mode 100644 index 000000000..3524f878e --- /dev/null +++ b/06week/functionalJavaScript/05basicEverySome.js @@ -0,0 +1,18 @@ +'use strict'; + +// Return a function that takes a list of valid users, and returns a function that returns true +// if all of the supplied users exist in the original list of users. +// +// You only need to check that the ids match. + +// Use array#some and Array#every to check every user passed to your returned function exists in the array passed to the exported function. + +const checkUsersValid = (goodUserArr) => { + return (submittedUsers) => { + return submittedUsers.every(everyUser => { + return goodUserArr.some(goodUser => goodUser.id === everyUser.id) + }); + }; +} + +module.exports = checkUsersValid diff --git a/06week/functionalJavaScript/06basicReduce.js b/06week/functionalJavaScript/06basicReduce.js new file mode 100644 index 000000000..350d005f7 --- /dev/null +++ b/06week/functionalJavaScript/06basicReduce.js @@ -0,0 +1,22 @@ +'use strict'; + +// Given an Array of strings, use Array#reduce to create an object that contains the number of times each string occured in the array. +// Return the object directly (no need to console.log). + +// Counting instances of values in an object + +const countWords = (inputWords) => { + let countedWords = inputWords.reduce((acc, words) => { + if (words in acc) { + // acc[words] = 0; + acc[words]++; + } else { + acc[words] = 1; + } + return acc; + }, {}); + + return countedWords; +} + +module.exports = countWords diff --git a/06week/functionalJavaScript/07basicRecursion.js b/06week/functionalJavaScript/07basicRecursion.js new file mode 100644 index 000000000..8e355c84e --- /dev/null +++ b/06week/functionalJavaScript/07basicRecursion.js @@ -0,0 +1,38 @@ +// Implement Array#reduce using recursion. +// +// To test your reduction works correctly we will use your reduce implementation to execute our solution +// to the previous basic_reduce problem. i.e. your reduce function will be passed an array of words, +// and a function, and an initial value which will return an object containing the counts for each word found in the array. +// You don't need to implement this functionality, it will be supplied to your reduce implementation. +// +// For simplicity, your implementation of reduce need not replicate the behaviour of a reduce missing an initial value. +// You may assume the initial value will always be supplied. + +const reduce = (arr, fn, initial) => { + return (reduceArr = (item, value) => { + if (item > arr.length - 1) { + return value; + } + return reduceArr(item + 1, fn(value, arr[item], item, arr)); + }) (0, initial); +}; + +// I got this solution from the intertnet so I could see the official solution and I was so close: +// function reduce(array, fn, init) { +// var arrayDup = array.slice(), i = 0; +// +// return (function recursiveReduce(arrayDup, fn, init) { +// return arrayDup.length ? recursiveReduce(arrayDup, fn, fn(init, arrayDup.shift()), i++, array) : init; +// }(arrayDup, fn, init)); +// } +// +// official solution: +// function reduce(arr, fn, initial) { +// return (function reduceOne(index, value) { +// if (index > arr.length - 1) return value // end condition +// return reduceOne(index + 1, fn(value, arr[index], index, arr)) // calculate & pass values to next step +// })(0, initial) // IIFE. kick off recursion with initial values +// } + + +module.exports = reduce diff --git a/06week/functionalJavaScript/08basicCall.js b/06week/functionalJavaScript/08basicCall.js new file mode 100644 index 000000000..e69de29bb diff --git a/06week/functionalJavaScript/09partialAppWoutBind.js b/06week/functionalJavaScript/09partialAppWoutBind.js new file mode 100644 index 000000000..e69de29bb diff --git a/06week/functionalJavaScript/10partialAppWBind.js b/06week/functionalJavaScript/10partialAppWBind.js new file mode 100644 index 000000000..e69de29bb diff --git a/06week/functionalJavaScript/11implementMapWReduce.js b/06week/functionalJavaScript/11implementMapWReduce.js new file mode 100644 index 000000000..e69de29bb diff --git a/06week/functionalJavaScript/12functionSpies.js b/06week/functionalJavaScript/12functionSpies.js new file mode 100644 index 000000000..e69de29bb diff --git a/06week/functionalJavaScript/13blockingEventLoop.js b/06week/functionalJavaScript/13blockingEventLoop.js new file mode 100644 index 000000000..e69de29bb diff --git a/06week/functionalJavaScript/14trampoline.js b/06week/functionalJavaScript/14trampoline.js new file mode 100644 index 000000000..e69de29bb diff --git a/06week/functionalJavaScript/15asyncLoops.js b/06week/functionalJavaScript/15asyncLoops.js new file mode 100644 index 000000000..e69de29bb diff --git a/06week/functionalJavaScript/16recursion.js b/06week/functionalJavaScript/16recursion.js new file mode 100644 index 000000000..e69de29bb diff --git a/06week/functionalJavaScript/17currying.js b/06week/functionalJavaScript/17currying.js new file mode 100644 index 000000000..e69de29bb diff --git a/06week/functionalJavaScript/18functionCall.js b/06week/functionalJavaScript/18functionCall.js new file mode 100644 index 000000000..e69de29bb