Last active
April 27, 2022 11:37
-
-
Save Londeren/f62518727e8218cf98466006514bf9d3 to your computer and use it in GitHub Desktop.
Javascript array map, reduce practice
This file contains 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
// 1. Implement `reduce` via forEach. Expected api for the function: | |
reduce(arr, (acc, item) => acc + item, 0); | |
// example of implementation: | |
function reduce(arr, reducerCallback, initialValue) { | |
let result; | |
……forEach(…) | |
return result; | |
} | |
// 2. Implement `map` via reduce | |
map([…], (item) => item + 1); | |
// 3. Implement `filter` via reduce | |
filter([…], (item) => item === 1); | |
// 4. Implement `some` and `every` via reduce | |
some([…], (item) => item === 1); | |
every([…], (item) => item === 1); | |
// 5. Implement `find` via reduce | |
find([…], (item) => item === 1); | |
/** | |
* 6. Functional Programming in Javascript | |
* @link http://reactivex.io/learnrx/ | |
*/ | |
/** | |
* 7. Quick JavaScript Array Functions Practice | |
* @link https://github.com/jamesqquick/javascript-array-functions-practice | |
*/ | |
/** | |
* 8. Practicing Arrays, ForEach, Map, Filter and Reduce | |
* @link https://gist.github.com/tgmarinho/f1a145d7c7c87fc495181ba21cf3681f | |
*/ | |
// Simple problems to solve using the native .reduce and .map array methods. Each of these problems can be solved in many | |
// different ways, but try to solve them using the requested higher order function. | |
// MAP | |
// Write a function capitalize that takes a string and uses .map to return the same string in all caps. | |
// ex. capitalize('whoop') // => 'WHOOP' | |
// ex. capitalize('oh hey gurl') // => "OH HEY GURL" | |
var capitalize = function(string){ | |
// code code code! | |
} | |
// Now write a new function called swapCase that takes a string of words and uses .map and your newly written capitalize() | |
// function to return a string where every other word is in all caps. | |
// Hint: look up Array.prototype.map on MDN and see what arguments the .map callback can take. | |
// ex: swapCase('hey gurl, lets javascript together sometime') // => "HEY gurl, LETS javascript TOGETHER sometime" | |
var swapCase = function(string){ | |
// Codeeeee | |
} | |
// Write a function shiftLetters that takes a string and uses .map to return an encoded string with each letter shifted down the | |
// alphabet by one. Hint: Use Look up the JS functions String.fromCharCode() and String.CharCodeAt() and see if you can use | |
// Ascii code to acomplish this. | |
// ex. shiftLetters('hello') // => 'ifmmp' | |
// ex. (shiftLetters('abcxyz') // => "bcdyz{" | |
var shiftLetters = function(string){ | |
// code! | |
} | |
// REDUCE | |
// Write a function that takes a string and returns an object representing the character | |
// count for each letter. Use .reduce to build this object. | |
// ex. countLetters('abbcccddddeeeee') // => {a:1, b:2, c:3, d:4, e:5} | |
var countLetters = function(string){ | |
// your code here | |
}; | |
// Write a function that takes a string and a target, and returns true or false if the target is present in the string. Use | |
// .reduce to acomplish this. | |
// ex. isPresent('abcd', 'b') // => true | |
// ex. isPresent('efghi', 'a') // => false | |
var isPresent = function(string, target) { | |
// GO GO GADGET CODE! | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment