From 36363199cbe4d81f6b7dcc22d4302dedbadd680a Mon Sep 17 00:00:00 2001 From: deytonk Date: Sat, 11 Nov 2017 00:07:18 -0600 Subject: [PATCH 1/4] basic map --- 06week/functionalJavaScript/01helloWorld.js | 6 +++++ 06week/functionalJavaScript/02higherOrder.js | 15 +++++++++++++ 06week/functionalJavaScript/03basicMap.js | 22 +++++++++++++++++++ 06week/functionalJavaScript/04basicFilter.js | 0 .../functionalJavaScript/05basicEverySome.js | 0 06week/functionalJavaScript/06basicReduce.js | 21 ++++++++++++++++++ 06week/functionalJavaScript/07basicRecursion | 0 06week/functionalJavaScript/08basicCall | 0 .../functionalJavaScript/09partialAppWoutBind | 0 06week/functionalJavaScript/10partialAppWBind | 0 .../11implementMapWReduce | 0 06week/functionalJavaScript/12functionSpies | 0 12 files changed, 64 insertions(+) create mode 100644 06week/functionalJavaScript/01helloWorld.js create mode 100644 06week/functionalJavaScript/02higherOrder.js create mode 100644 06week/functionalJavaScript/03basicMap.js create mode 100644 06week/functionalJavaScript/04basicFilter.js create mode 100644 06week/functionalJavaScript/05basicEverySome.js create mode 100644 06week/functionalJavaScript/06basicReduce.js create mode 100644 06week/functionalJavaScript/07basicRecursion create mode 100644 06week/functionalJavaScript/08basicCall create mode 100644 06week/functionalJavaScript/09partialAppWoutBind create mode 100644 06week/functionalJavaScript/10partialAppWBind create mode 100644 06week/functionalJavaScript/11implementMapWReduce create mode 100644 06week/functionalJavaScript/12functionSpies diff --git a/06week/functionalJavaScript/01helloWorld.js b/06week/functionalJavaScript/01helloWorld.js new file mode 100644 index 000000000..a0caeaeda --- /dev/null +++ b/06week/functionalJavaScript/01helloWorld.js @@ -0,0 +1,6 @@ +// 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..37a148e43 --- /dev/null +++ b/06week/functionalJavaScript/02higherOrder.js @@ -0,0 +1,15 @@ +// 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..5ae076a57 --- /dev/null +++ b/06week/functionalJavaScript/03basicMap.js @@ -0,0 +1,22 @@ +// 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(function(i) { + result.push(i * 2); + }); + + return result; + +} + +module.exports = doubleAll diff --git a/06week/functionalJavaScript/04basicFilter.js b/06week/functionalJavaScript/04basicFilter.js new file mode 100644 index 000000000..e69de29bb diff --git a/06week/functionalJavaScript/05basicEverySome.js b/06week/functionalJavaScript/05basicEverySome.js new file mode 100644 index 000000000..e69de29bb diff --git a/06week/functionalJavaScript/06basicReduce.js b/06week/functionalJavaScript/06basicReduce.js new file mode 100644 index 000000000..9c64ab6f0 --- /dev/null +++ b/06week/functionalJavaScript/06basicReduce.js @@ -0,0 +1,21 @@ +// 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. + +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 diff --git a/06week/functionalJavaScript/07basicRecursion b/06week/functionalJavaScript/07basicRecursion new file mode 100644 index 000000000..e69de29bb diff --git a/06week/functionalJavaScript/08basicCall b/06week/functionalJavaScript/08basicCall new file mode 100644 index 000000000..e69de29bb diff --git a/06week/functionalJavaScript/09partialAppWoutBind b/06week/functionalJavaScript/09partialAppWoutBind new file mode 100644 index 000000000..e69de29bb diff --git a/06week/functionalJavaScript/10partialAppWBind b/06week/functionalJavaScript/10partialAppWBind new file mode 100644 index 000000000..e69de29bb diff --git a/06week/functionalJavaScript/11implementMapWReduce b/06week/functionalJavaScript/11implementMapWReduce new file mode 100644 index 000000000..e69de29bb diff --git a/06week/functionalJavaScript/12functionSpies b/06week/functionalJavaScript/12functionSpies new file mode 100644 index 000000000..e69de29bb From e42b1167c5c607212194a920b7385aed4777f1f0 Mon Sep 17 00:00:00 2001 From: deytonk Date: Mon, 13 Nov 2017 01:05:35 -0600 Subject: [PATCH 2/4] Progress from Office Hours --- 06week/functionalJavaScript/01helloWorld.js | 2 ++ 06week/functionalJavaScript/02higherOrder.js | 2 ++ 06week/functionalJavaScript/03basicMap.js | 16 +++++---- 06week/functionalJavaScript/04basicFilter.js | 33 +++++++++++++++++++ .../functionalJavaScript/05basicEverySome.js | 18 ++++++++++ 06week/functionalJavaScript/06basicReduce.js | 29 +++++++--------- .../{07basicRecursion => 07basicRecursion.js} | 0 .../{08basicCall => 08basicCall.js} | 0 ...ialAppWoutBind => 09partialAppWoutBind.js} | 0 ...10partialAppWBind => 10partialAppWBind.js} | 0 ...entMapWReduce => 11implementMapWReduce.js} | 0 .../{12functionSpies => 12functionSpies.js} | 0 .../13blockingEventLoop.js | 0 06week/functionalJavaScript/14trampoline.js | 0 06week/functionalJavaScript/15asyncLoops.js | 0 06week/functionalJavaScript/16recursion.js | 0 06week/functionalJavaScript/17currying.js | 0 06week/functionalJavaScript/18functionCall.js | 0 18 files changed, 77 insertions(+), 23 deletions(-) rename 06week/functionalJavaScript/{07basicRecursion => 07basicRecursion.js} (100%) rename 06week/functionalJavaScript/{08basicCall => 08basicCall.js} (100%) rename 06week/functionalJavaScript/{09partialAppWoutBind => 09partialAppWoutBind.js} (100%) rename 06week/functionalJavaScript/{10partialAppWBind => 10partialAppWBind.js} (100%) rename 06week/functionalJavaScript/{11implementMapWReduce => 11implementMapWReduce.js} (100%) rename 06week/functionalJavaScript/{12functionSpies => 12functionSpies.js} (100%) create mode 100644 06week/functionalJavaScript/13blockingEventLoop.js create mode 100644 06week/functionalJavaScript/14trampoline.js create mode 100644 06week/functionalJavaScript/15asyncLoops.js create mode 100644 06week/functionalJavaScript/16recursion.js create mode 100644 06week/functionalJavaScript/17currying.js create mode 100644 06week/functionalJavaScript/18functionCall.js diff --git a/06week/functionalJavaScript/01helloWorld.js b/06week/functionalJavaScript/01helloWorld.js index a0caeaeda..730126165 100644 --- a/06week/functionalJavaScript/01helloWorld.js +++ b/06week/functionalJavaScript/01helloWorld.js @@ -1,3 +1,5 @@ +'use strict'; + // Write a function that takes an input string and returns it uppercased. function upperCaser(input) { diff --git a/06week/functionalJavaScript/02higherOrder.js b/06week/functionalJavaScript/02higherOrder.js index 37a148e43..50a168147 100644 --- a/06week/functionalJavaScript/02higherOrder.js +++ b/06week/functionalJavaScript/02higherOrder.js @@ -1,3 +1,5 @@ +'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. diff --git a/06week/functionalJavaScript/03basicMap.js b/06week/functionalJavaScript/03basicMap.js index 5ae076a57..d61613e9b 100644 --- a/06week/functionalJavaScript/03basicMap.js +++ b/06week/functionalJavaScript/03basicMap.js @@ -1,3 +1,5 @@ +'use strict'; + // Convert the following code from a for-loop to Array#map: // function doubleAll(numbers) { @@ -8,15 +10,17 @@ // return result // } - const doubleAll = (numbers) => { - let result = []; - numbers.map(function(i) { - result.push(i * 2); - }); + // let result = []; + // numbers.map(i => result.push(i * 2)); + + return numbers.map(i => i * 2); - return result; + // 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 index e69de29bb..ae93a840c 100644 --- a/06week/functionalJavaScript/04basicFilter.js +++ 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 index e69de29bb..3524f878e 100644 --- a/06week/functionalJavaScript/05basicEverySome.js +++ 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 index 9c64ab6f0..ed1b30a66 100644 --- a/06week/functionalJavaScript/06basicReduce.js +++ b/06week/functionalJavaScript/06basicReduce.js @@ -1,21 +1,16 @@ -// 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. +'use strict'; -function getShortMessages(messages) { +// 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). - let shortMessage = messages.filter(function(shorten) { - if (shorten.message.length < 50) { - return true; - } - }); - - return shortMessage.map(function(shorten) { - return shorten.message; - }) +const countWords = (inputWords) => { + // let obj = {}; + inputWords.reduce((acc, i) => { + if (!acc[i]) { + acc[i] = 0; + acc[i]++; + return acc; + } , {}}); } -module.exports = getShortMessages +module.exports = countWords diff --git a/06week/functionalJavaScript/07basicRecursion b/06week/functionalJavaScript/07basicRecursion.js similarity index 100% rename from 06week/functionalJavaScript/07basicRecursion rename to 06week/functionalJavaScript/07basicRecursion.js diff --git a/06week/functionalJavaScript/08basicCall b/06week/functionalJavaScript/08basicCall.js similarity index 100% rename from 06week/functionalJavaScript/08basicCall rename to 06week/functionalJavaScript/08basicCall.js diff --git a/06week/functionalJavaScript/09partialAppWoutBind b/06week/functionalJavaScript/09partialAppWoutBind.js similarity index 100% rename from 06week/functionalJavaScript/09partialAppWoutBind rename to 06week/functionalJavaScript/09partialAppWoutBind.js diff --git a/06week/functionalJavaScript/10partialAppWBind b/06week/functionalJavaScript/10partialAppWBind.js similarity index 100% rename from 06week/functionalJavaScript/10partialAppWBind rename to 06week/functionalJavaScript/10partialAppWBind.js diff --git a/06week/functionalJavaScript/11implementMapWReduce b/06week/functionalJavaScript/11implementMapWReduce.js similarity index 100% rename from 06week/functionalJavaScript/11implementMapWReduce rename to 06week/functionalJavaScript/11implementMapWReduce.js diff --git a/06week/functionalJavaScript/12functionSpies b/06week/functionalJavaScript/12functionSpies.js similarity index 100% rename from 06week/functionalJavaScript/12functionSpies rename to 06week/functionalJavaScript/12functionSpies.js 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 From c93a6d12874f24b709d331b18a5ef23b70991ca9 Mon Sep 17 00:00:00 2001 From: deytonk Date: Tue, 14 Nov 2017 12:23:41 -0600 Subject: [PATCH 3/4] Passing 6 :fish: --- 06week/functionalJavaScript/06basicReduce.js | 18 ++++++++++++------ .../functionalJavaScript/07basicRecursion.js | 15 +++++++++++++++ 2 files changed, 27 insertions(+), 6 deletions(-) diff --git a/06week/functionalJavaScript/06basicReduce.js b/06week/functionalJavaScript/06basicReduce.js index ed1b30a66..350d005f7 100644 --- a/06week/functionalJavaScript/06basicReduce.js +++ b/06week/functionalJavaScript/06basicReduce.js @@ -3,14 +3,20 @@ // 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 obj = {}; - inputWords.reduce((acc, i) => { - if (!acc[i]) { - acc[i] = 0; - acc[i]++; + 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 index e69de29bb..3bb9a50b5 100644 --- a/06week/functionalJavaScript/07basicRecursion.js +++ b/06week/functionalJavaScript/07basicRecursion.js @@ -0,0 +1,15 @@ +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. + +function reduce(arr, fn, initial) { + +} + +module.exports = reduce From 67e34e422f9fa976de22da8b8c0fc653a164bc4e Mon Sep 17 00:00:00 2001 From: deytonk Date: Tue, 14 Nov 2017 18:46:18 -0600 Subject: [PATCH 4/4] progress before class --- .../functionalJavaScript/07basicRecursion.js | 43 ++++++++++++++----- 1 file changed, 33 insertions(+), 10 deletions(-) diff --git a/06week/functionalJavaScript/07basicRecursion.js b/06week/functionalJavaScript/07basicRecursion.js index 3bb9a50b5..8e355c84e 100644 --- a/06week/functionalJavaScript/07basicRecursion.js +++ b/06week/functionalJavaScript/07basicRecursion.js @@ -1,15 +1,38 @@ -Implement Array#reduce using recursion. +// 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. -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. +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); +}; -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. +// 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 +// } -function reduce(arr, fn, initial) { - -} module.exports = reduce