From fb986a7ec7fa9cb5a9bbae24f8b1f4de119ccb44 Mon Sep 17 00:00:00 2001 From: Andrew Peterson Date: Thu, 23 Jan 2020 14:30:16 -0800 Subject: [PATCH 1/3] create a new branch for higher order function challenges --- .vscode/launch.json | 22 +++++++++ src/index.ts | 107 +++++++++++++++++++++++++++++++++++++++----- src/testing.ts | 55 +++++++++++++++++++++++ 3 files changed, 172 insertions(+), 12 deletions(-) create mode 100644 .vscode/launch.json create mode 100644 src/testing.ts diff --git a/.vscode/launch.json b/.vscode/launch.json new file mode 100644 index 0000000..a515037 --- /dev/null +++ b/.vscode/launch.json @@ -0,0 +1,22 @@ +{ + // Use IntelliSense to learn about possible attributes. + // Hover to view descriptions of existing attributes. + // For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387 + "version": "0.2.0", + "configurations": [ + + { + "type": "node", + "request": "launch", + "name": "Launch Program", + "skipFiles": [ + "/**" + ], + "program": "${workspaceFolder}/index.js", + "preLaunchTask": "tsc: build - tsconfig.json", + "outFiles": [ + "${workspaceFolder}/dist/**/*.js" + ] + } + ] +} \ No newline at end of file diff --git a/src/index.ts b/src/index.ts index fd55cff..d45f5d7 100644 --- a/src/index.ts +++ b/src/index.ts @@ -1,4 +1,7 @@ -const pets = [ +import * as tests from "./testing" + + +export const pets = [ { name: "fido", type: "dog", @@ -20,7 +23,7 @@ const pets = [ dangerLevel: 5, }, { - name: "CatStevns", + name: "CatStevens", type: "cat", dangerLevel: 7, }, @@ -36,25 +39,105 @@ const pets = [ }, ] + +// New Array +// write a function that returns an alphabetized list of animal names. + +export const alphabetizePets = (petArray) => { + const petNameArray = petArray.map(x => x.name) + const alphabetizedArray = petNameArray.sort(function (a, b) { + return a.toLowerCase().localeCompare(b.toLowerCase()) + }) + return alphabetizedArray +} + +// take the array of pets and return a single object where the eys are the animal name and the values are the corresponding animal object with their danger level multiplied by 2. + +export const doubleDanger = (petArray) => { + const object = petArray.reduce( + (obj, item) => Object.assign(obj, { [item.name]: (item.dangerLevel) * 2 }), {} + ) + return object +} + // what is the most dangerous animal? // 1. Write a function that returns the name of the most dangerous animal. -// 2. Write a function that returns the name of the least dangerous animal. -// 3. Write a function that returns an object containing only type and danger level of the most dangerous animal. + +export const findDanger = (animalArray) => { + return animalArray.reduce( + (acc, ele) => { + return acc.dangerLevel < ele.dangerLevel ? ele : acc + }).name +} + +// // 2. Write a function that returns the name of the least dangerous animal. + +export const findLeastDanger = (animalArray) => { + return animalArray.reduce( + (accumalate, element) => { + return accumalate.dangerLevel > element.dangerLevel ? element : accumalate + }).name +} + +// // 3. Write a function that returns an object containing only type and danger level of the most dangerous animal. + +export const findDangerAndType = (animalArray) => { + let mostDangerousAnimal = animalArray.reduce( + (accumalate, element) => { + return accumalate.dangerLevel < element.dangerLevel ? element : accumalate + } + ) + return { dangerLevel: mostDangerousAnimal.dangerLevel, type: mostDangerousAnimal.type } +} // Who would win? // 1. Write a function that takes two numbers (0-6). The numbers represent the index of the pet in the pets array. Write a function that returns the animal which the highest danger level between the two pets. +// [rido, Dido] + +export const animalFight = (num1, num2, animals) => { + + const result = animals[num1].dangerLevel > animals[num2].dangerLevel ? animals[num1].name : animals[num2].name + return result +} + // 2. write a function that takes two names +export const animalFightByName = (name1, name2, animals) => { + const fighters = animals.filter( + (animal) => { + if (animal.name === name1 || animal.name === name2) { + return true + } + return false + }) + + return animalFight(0, 1, fighters) +} + // how many of each pet type there is? // 1. Write a function that takes an array of pets (like the one defined at the top of this file) and returns a new object that shows the count of each type of animal. It should look like this: -const petCount = { - dog: 4, - cat: 2, - locust: 1, - // [pet.type]: count + + +export const animalTypeCountFilter = (petArray) => { + return petArray.reduce((acc, pet) => { + const { type } = pet + const accTypeExists = acc[type] + return accTypeExists ? { ...acc, [type]: acc[type] + 1 } : { ...acc, [type]: 1 } + }, {}) } -// New Array -// write a function that returns an alphabetized list of animal names. +console.log(tests.testAnimalCount() + " testing for animal type count") + +console.log(tests.testAnimalFightByName() + " testing animal fight by name") + +console.log(tests.testDoubleDanger() + " double danger test"); + +console.log(tests.testFindDanger() + " testing find most danger") + +console.log(tests.testFindLeastDanger() + " testing find least danger") + +console.log(tests.testFindDangerAndType() + " test find danger and type"); + +console.log(tests.testAlphabetizePets() + " test alphabetize pet list") -console.log("TEST") +console.log(tests.testAnimalFight() + " testing animal fight") \ No newline at end of file diff --git a/src/testing.ts b/src/testing.ts new file mode 100644 index 0000000..a1a1823 --- /dev/null +++ b/src/testing.ts @@ -0,0 +1,55 @@ +import * as indexStuff from "./index" + +export const testTheThing = (actual, expected) => { + return actual === expected + ? "Good test" + : "Bad test! Expected: " + expected + " but received: " + actual +} + +export const testAnimalFightByName = () => { + const actual = indexStuff.animalFightByName("rido", "Dido", indexStuff.pets) + const expected = "rido" + return testTheThing(actual, expected) +} + +export const testDoubleDanger = () => { + const actual = indexStuff.doubleDanger(indexStuff.pets).rido + const expected = 24 + return testTheThing(actual, expected) +} + +export const testFindDanger = () => { + const actual = indexStuff.findDanger(indexStuff.pets) + const expected = "rido" + return testTheThing(actual, expected) +} + +export const testFindLeastDanger = () => { + const actual = indexStuff.findLeastDanger(indexStuff.pets) + const expected = "fido" + return testTheThing(actual, expected) +} + +export const testFindDangerAndType = () => { + const actual = indexStuff.findDangerAndType(indexStuff.pets).dangerLevel + " " + indexStuff.findDangerAndType(indexStuff.pets).type + const expected = indexStuff.pets[1].dangerLevel + " " + indexStuff.findDangerAndType(indexStuff.pets).type + return testTheThing(actual, expected) +} + +export const testAnimalFight = () => { + const actual = indexStuff.animalFight(0, 4, indexStuff.pets) + const expected = "CatStevens" + return testTheThing(actual, expected) +} + +export const testAlphabetizePets = () => { + const actual = indexStuff.alphabetizePets(indexStuff.pets)[0] + const expected = 'CatStevens' + return testTheThing(actual, expected) +} + +export const testAnimalCount = () => { + const actual = indexStuff.animalTypeCountFilter(indexStuff.pets).dog + const expected = 4 + return testTheThing(actual, expected) +} \ No newline at end of file From 09cf622922d64dc7b05af39eda3db397f9ac683e Mon Sep 17 00:00:00 2001 From: Andrew Peterson Date: Thu, 23 Jan 2020 15:09:13 -0800 Subject: [PATCH 2/3] remove 'test' from testing console log string --- src/index.ts | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/src/index.ts b/src/index.ts index d45f5d7..2b2e170 100644 --- a/src/index.ts +++ b/src/index.ts @@ -126,18 +126,18 @@ export const animalTypeCountFilter = (petArray) => { }, {}) } -console.log(tests.testAnimalCount() + " testing for animal type count") +console.log(tests.testAnimalCount() + " for animal type count") -console.log(tests.testAnimalFightByName() + " testing animal fight by name") +console.log(tests.testAnimalFightByName() + " animal fight by name") console.log(tests.testDoubleDanger() + " double danger test"); -console.log(tests.testFindDanger() + " testing find most danger") +console.log(tests.testFindDanger() + " find most danger") -console.log(tests.testFindLeastDanger() + " testing find least danger") +console.log(tests.testFindLeastDanger() + " find least danger") -console.log(tests.testFindDangerAndType() + " test find danger and type"); +console.log(tests.testFindDangerAndType() + " find danger and type"); -console.log(tests.testAlphabetizePets() + " test alphabetize pet list") +console.log(tests.testAlphabetizePets() + " alphabetize pet list") -console.log(tests.testAnimalFight() + " testing animal fight") \ No newline at end of file +console.log(tests.testAnimalFight() + " animal fight") \ No newline at end of file From 644c6d03d8c6c862b7a4b4674cc2105d953e9e92 Mon Sep 17 00:00:00 2001 From: Andrew Peterson Date: Mon, 27 Jan 2020 11:04:29 -0800 Subject: [PATCH 3/3] make changes suggested by frank in PR --- src/index.ts | 17 ++++++++--------- src/testing.ts | 2 +- 2 files changed, 9 insertions(+), 10 deletions(-) diff --git a/src/index.ts b/src/index.ts index 2b2e170..0222ba0 100644 --- a/src/index.ts +++ b/src/index.ts @@ -1,6 +1,5 @@ import * as tests from "./testing" - export const pets = [ { name: "fido", @@ -39,7 +38,6 @@ export const pets = [ }, ] - // New Array // write a function that returns an alphabetized list of animal names. @@ -51,12 +49,15 @@ export const alphabetizePets = (petArray) => { return alphabetizedArray } -// take the array of pets and return a single object where the eys are the animal name and the values are the corresponding animal object with their danger level multiplied by 2. +// take the array of pets and return a single object where the keys are the animal name and the values are the corresponding animal object with their danger level multiplied by 2. export const doubleDanger = (petArray) => { - const object = petArray.reduce( - (obj, item) => Object.assign(obj, { [item.name]: (item.dangerLevel) * 2 }), {} - ) + const object = petArray.reduce((acc, item) => { + return { + ...acc, + [item.name]: (item.dangerLevel) * 2, + } + }) return object } @@ -82,7 +83,7 @@ export const findLeastDanger = (animalArray) => { // // 3. Write a function that returns an object containing only type and danger level of the most dangerous animal. export const findDangerAndType = (animalArray) => { - let mostDangerousAnimal = animalArray.reduce( + const mostDangerousAnimal = animalArray.reduce( (accumalate, element) => { return accumalate.dangerLevel < element.dangerLevel ? element : accumalate } @@ -95,7 +96,6 @@ export const findDangerAndType = (animalArray) => { // [rido, Dido] export const animalFight = (num1, num2, animals) => { - const result = animals[num1].dangerLevel > animals[num2].dangerLevel ? animals[num1].name : animals[num2].name return result } @@ -110,7 +110,6 @@ export const animalFightByName = (name1, name2, animals) => { } return false }) - return animalFight(0, 1, fighters) } diff --git a/src/testing.ts b/src/testing.ts index a1a1823..568e448 100644 --- a/src/testing.ts +++ b/src/testing.ts @@ -3,7 +3,7 @@ import * as indexStuff from "./index" export const testTheThing = (actual, expected) => { return actual === expected ? "Good test" - : "Bad test! Expected: " + expected + " but received: " + actual + : `Bad test! Expected: ${expected} but received: ${actual}` } export const testAnimalFightByName = () => {