Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Complete higher order function challenges #ticketnumber #2

Open
wants to merge 3 commits into
base: animal-problems-solution-2
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 22 additions & 0 deletions .vscode/launch.json
Original file line number Diff line number Diff line change
@@ -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": [
"<node_internals>/**"
],
"program": "${workspaceFolder}/index.js",
"preLaunchTask": "tsc: build - tsconfig.json",
"outFiles": [
"${workspaceFolder}/dist/**/*.js"
]
}
]
}
106 changes: 94 additions & 12 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
const pets = [
import * as tests from "./testing"

export const pets = [
{
name: "fido",
type: "dog",
Expand All @@ -20,7 +22,7 @@ const pets = [
dangerLevel: 5,
},
{
name: "CatStevns",
name: "CatStevens",
type: "cat",
dangerLevel: 7,
},
Expand All @@ -36,25 +38,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())
})
drewcpete marked this conversation as resolved.
Show resolved Hide resolved
return alphabetizedArray
}

// 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((acc, item) => {
return {
...acc,
[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) => {
const 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 }
}, {})
drewcpete marked this conversation as resolved.
Show resolved Hide resolved
}

// New Array
// write a function that returns an alphabetized list of animal names.
console.log(tests.testAnimalCount() + " for animal type count")

console.log(tests.testAnimalFightByName() + " animal fight by name")

console.log(tests.testDoubleDanger() + " double danger test");

console.log(tests.testFindDanger() + " find most danger")

console.log(tests.testFindLeastDanger() + " find least danger")

console.log(tests.testFindDangerAndType() + " find danger and type");

console.log(tests.testAlphabetizePets() + " alphabetize pet list")

console.log("TEST")
console.log(tests.testAnimalFight() + " animal fight")
55 changes: 55 additions & 0 deletions src/testing.ts
Original file line number Diff line number Diff line change
@@ -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)
}