Last active
November 15, 2016 14:48
-
-
Save mrosata/34b337c9f712bebe2c662f5cc55f86f3 to your computer and use it in GitHub Desktop.
These files accompany my "Map Filter Reduce" videos found at https://www.youtube.com/watch?v=qTeeVd8hOFY
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
| console.clear(); | |
| /** | |
| * Video: "JavaScript Array superpowers: Map, Filter, Reduce" | |
| * URL: https://www.youtube.com/watch?v=qTeeVd8hOFY | |
| * | |
| * These 2 exercises test your knowledge and use of the map method. | |
| * Remember, mapping creates a new array from an existing array with | |
| * a 1 to 1 relationship in length. | |
| * | |
| * Map :: [a,a,a] -> [b,b,b] | |
| * | |
| * Your missions, should you choose to accept them: | |
| * | |
| * 1. create a function that will return an array of all the user | |
| * names in the users objects array using Array.prototype.map | |
| * or a custom built map implementation. | |
| * | |
| * 2. We need to update all the user ids to start in the | |
| * thousands. The tech guys wrote a function to determine | |
| * what number to assign as new ids, you just need to build | |
| * a new users array that contains the same users we have | |
| * now, but with their updated ids. | |
| * | |
| * ***EXTRA** Because of the nature of objects in JavaScript, | |
| * when you update the user objects they will update in both | |
| * the new array and the old one. This is a topic of wide | |
| * debate over the past couple years. For extra credit see if | |
| * you can create new user objects in example 2, so that the | |
| * end result leaves the original `users` array below | |
| * unchanged, or "not mutated". | |
| * | |
| */ | |
| const assert = new BSMeter(); | |
| const users = [ | |
| {name: 'Michael', id: 19, gender: 'male', friends: [2, 53, 29, 210]}, | |
| {name: 'Kyle', id: 2, gender: 'male', friends: []}, | |
| {name: 'Christina', id: 53, gender: 'female', friends: [19, 2, 210]}, | |
| {name: 'Sarah', id: 122, gender: 'female', friends: [19, 60, 53, 29]}, | |
| {name: 'Juno', id: 29, gender: 'female', friends: [319]}, | |
| {name: 'Erik', id: 319, gender: 'male', friends: [2, 19, 60, 29, 3]}, | |
| {name: 'Juan', id: 210, gender: 'male', friends: [19, 60, 319]}, | |
| {name: 'Joanna', id: 60, gender: 'female', friends: [2, 210, 103]}, | |
| {name: 'Pete', id: 112, gender: 'male', friends: []}, | |
| {name: 'Sarah', id: 3, gender: 'female', friends: [19, 2, 53, 29]}, | |
| ]; | |
| /**** Write your code under here: ****/ | |
| /* #1. Create the array of user names */ | |
| let listOfNames = []; // replace empty array with you own code | |
| /* #2. Create a new array with all users and their friends updated to | |
| the new id system. Use the function `updateUserId( oldId )` | |
| to change the numbers IE: updateUserId(39) might return 1039 */ | |
| let updatedUsers = []; // replace empty array with your own code. | |
| /**** ******* ******* ******* ******* | |
| ****** ******* ******* ******* ***** | |
| * THE END ( Below is code simply meant to test your solution) | |
| ******* ******* ******* ******* **** | |
| ******* ******* ******* ******* **/ | |
| // Helper function to update ids. | |
| function updateUserId(idNumber) { | |
| return idNumber + 1234; | |
| } | |
| // Test Suite for example #1. | |
| assert.suite(function() { | |
| assert.is.array(listOfNames, 'Should have an array for names'); | |
| assert.equals(listOfNames.length, users.length, | |
| "Both arrays should have same length"); | |
| assert.includes(listOfNames, users[0].name, 'should have all user names'); | |
| assert.includes(listOfNames, users[4].name, 'should have all user names'); | |
| assert.includes(listOfNames, users[5].name, 'should have all user names'); | |
| assert.includes(listOfNames, users[6].name, 'should have all user names'); | |
| assert.equiv(listOfNames.toString(), | |
| 'Michael,Kyle,Christina,Sarah,Juno,Erik,Juan,Joanna,Pete,Sarah'); | |
| }, 'Wow! You handled that problem! Are there any subroutines in your family tree?'); | |
| // Test Suite for example #2. | |
| assert.suite(function() { | |
| assert.is.array(updatedUsers, "should have updated users array"); | |
| assert.is.object(updatedUsers[0], "should have user objects") | |
| assert.equals(updatedUsers[0].id, 1253); | |
| assert.equals(updatedUsers[1].id, 1236); | |
| assert.equals(updatedUsers[6].id, 1444); | |
| assert.equals(updatedUsers[9].id, 1237); | |
| assert.equals(updatedUsers[2].friends.length, 3); | |
| assert.is.array(updatedUsers[8].friends); | |
| assert.equals(updatedUsers[8].friends.length, 0); | |
| assert.includes(updatedUsers[3].friends, 1294, | |
| 'updated users should have updated friends ids'); | |
| assert.includes(updatedUsers[4].friends, 1553, | |
| 'updated users should have updated friends ids'); | |
| assert.includes(updatedUsers[9].friends, 1236, | |
| 'updated users should have updated friends ids'); | |
| // If all tests pass then let the programmer know they are awesome! | |
| }, 'Congrats! If your reading this (in the console) You Passed!'); | |
| assert.suite(function() { | |
| // Extra credit tests. | |
| assert.is.array(users, | |
| 'original users array should exist'); | |
| assert.is.array(users[3].friends, | |
| 'original user friends arrays should exist'); | |
| assert.equals(users[3].friends.length, updatedUsers[3].friends.length, | |
| 'the users old and new friends lists should be same length'); | |
| assert.equals(users[1].id, 2, | |
| 'original user object should not mutate.'); | |
| assert.equals(users[5].id, 319, | |
| 'original user object should not mutate.'); | |
| assert.equals(users[7].id, 60, | |
| 'original user object should not mutate.'); | |
| assert.equals(users[2].friends[0], 19, | |
| 'original friends object should not mutate.'); | |
| assert.includes(users[6].friends, 319, | |
| 'original friends object should not mutate.'); | |
| assert.includes(users[7].friends, 210, | |
| 'original friends object should not mutate.'); | |
| // Let the programmer know we are impressed! | |
| }, 'WHOA! You got the extra credit! You the program!'); | |
| /*****************************************************************/ | |
| /* **/ | |
| /* Babies First Testing Suite: **/ | |
| /* suitable for ages ES6 + up **/ | |
| /* | |
| /* The tests you need to pass are already written, they use | |
| /* this micro-library to assert that your final results match | |
| /* a predetermined outcome. You don't have to worry about the | |
| /* internals of the testing suite, but of coarse if your | |
| /* curious then have at it! | |
| /* | |
| /* | |
| /* BSMeter Testing Suite: MIT License | |
| /** | |
| * Creates a new BSMeter assertion object. | |
| * | |
| * @class | |
| * @classdesc | |
| * | |
| * ```javascript | |
| * // First we create a new BSMeter assertion object. | |
| * const assert = new BSMeter(); | |
| * // Make a test... if it's all good, silence... | |
| * assert.equal(10, 10); | |
| * // Make a bad assertion and it throws a failure message! | |
| * assert.equal(10, 100); | |
| * // Include custom messages and they will be included. | |
| * assert.equal(1, 10, 'one should be one'); | |
| * | |
| * // .equal checks that a === b | |
| * assert.equals(a, b) | |
| * // .equiv checks if a == b | |
| * assert.equiv(a, b); | |
| * // .includes checks if `item` is in `array` | |
| * assert.includes(array, item); | |
| * // .hasProp checks if `obj` has prop named `prop` | |
| * assert.hasProp(obj, prop); | |
| * // .propEquals is just .hasProp then .equals | |
| * assert.propEquals(obj, prop, val); | |
| * // .propEquiv is same as .hasProp then .equiv | |
| * assert.propEquals(obj, prop, val); | |
| * ``` | |
| */ | |
| function BSMeter () { | |
| /* Assign bsMeterFailure messages before throwing */ | |
| /* or error messages in console show actual strings. */ | |
| /**@private */ | |
| let testSuitesRan = 0; | |
| /**@private */ | |
| function nextSuiteNumber() { | |
| return testSuitesRan++; | |
| } | |
| /**@private */ | |
| function _logFail(msg) { | |
| throw new AssertionError(`Detected Fail; ${msg}`); | |
| } | |
| const isms = { | |
| array(val, msg = '') { | |
| if (!Array.isArray(val)) { | |
| throw new AssertionError( | |
| `Expected Array; Got ${(typeof val).toUpperCase()}; ${msg}`); | |
| } | |
| }, | |
| object(val, msg = '') { | |
| if (!val || typeof val !== "object") { | |
| throw new AssertionError( | |
| `Expected Object; Got ${(typeof val).toUpperCase()}; ${msg}`); | |
| } | |
| } | |
| }; | |
| return { | |
| /** @memberof BSMeter# */ | |
| is: isms, | |
| /** @memberof BSMeter# */ | |
| equals(a, b, msg = '') { | |
| if (a !== b) { | |
| return _logFail(`Expected ${a} to equal ${b}. ${msg}`); | |
| } | |
| }, | |
| /** @memberof BSMeter# */ | |
| equiv(a, b, msg = '') { | |
| if (a != b) { | |
| return _logFail(`Expected ${a} to equal ${b}. ${msg}`); | |
| } | |
| }, | |
| /** @memberof BSMeter# */ | |
| includes(array, item, msg = '') { | |
| isms.array(array); | |
| if (!array.includes(item)) { | |
| return _logFail( | |
| `Expected ${item} to be included in array: ${array}`); | |
| } | |
| }, | |
| /** @memberof BSMeter# */ | |
| hasProp(obj, prop, msg = '') { | |
| isms.object(obj); | |
| if (!obj.hasOwnProperty(prop)) { | |
| return _logFail( | |
| `Expected property '${prop}' to be in object: ${obj}`); | |
| } | |
| }, | |
| /** @memberof BSMeter# */ | |
| propEquals(obj, prop, val, msg) { | |
| this.hasProp(obj, prop, val, msg); | |
| return this.equals(obj[prop], val, msg); | |
| }, | |
| /** @memberof BSMeter# */ | |
| propEquiv(obj, prop, val, msg) { | |
| this.hasProp(obj, prop, val, msg); | |
| return this.equiv(obj[prop], val, msg); | |
| }, | |
| /** @memberof BSMeter# */ | |
| suite(testWrappedInFn, successMsg='') { | |
| const suiteNumber = nextSuiteNumber(); | |
| try { | |
| console.log(`Starting Test Suite #${suiteNumber}`); | |
| testWrappedInFn(); | |
| console.log(`Test Suite #${suiteNumber} PASSED... ${successMsg}`); | |
| } | |
| catch (error) { | |
| console.log(error.message); | |
| console.log(`Test suite #${suiteNumber} FAILED...`); | |
| throw "This is the end... my only friend.. the end."; | |
| } | |
| } | |
| }; | |
| } | |
| function AssertionError(errorMsg) { | |
| this.message = errorMsg; | |
| this.name = "AssertionError"; | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment