Skip to content

Commit

Permalink
fix: Solutions
Browse files Browse the repository at this point in the history
  • Loading branch information
wearethefoos committed Oct 6, 2023
1 parent 976c784 commit 9aa7382
Show file tree
Hide file tree
Showing 10 changed files with 192 additions and 344 deletions.
24 changes: 12 additions & 12 deletions 1.map-filter-find.js
Original file line number Diff line number Diff line change
@@ -1,26 +1,26 @@
const getPokeNames = (pokemons) => {
return pokemons.map(pokemon => pokemon.name)
}
return pokemons.map((pokemon) => pokemon.name);
};

const getPokemonById = (pokemons, id) => {
return pokemons.find(pokemon => pokemon.id === id)
}
return pokemons.find((pokemon) => pokemon.id === id);
};

const getRarePokemons = (pokemons) => {
return pokemons.filter(pokemon => pokemon.spawn_chance < 0.10)
}
return pokemons.filter((pokemon) => pokemon.spawn_chance < 0.1);
};

const getMidSizedPokemon = (pokemons) => {
return pokemons.find(pokemon => pokemon.weight === "38.0 kg")
}
return pokemons.find((pokemon) => pokemon.weight === "38.0 kg");
};

const getAdultPokemons = (pokemons) => {
return pokemons.filter(pokemon => pokemon.egg === "Not in Eggs")
}
return pokemons.filter((pokemon) => pokemon.egg === "Not in Eggs");
};

const getPokemonImages = (pokemons) => {
return pokemons.map(pokemon => pokemon.img)
}
return pokemons.map((pokemon) => pokemon.img);
};

module.exports = {
getPokeNames,
Expand Down
12 changes: 6 additions & 6 deletions 1.map-filter-find.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,15 +9,15 @@ const {
} = require("./1.map-filter-find");

test("getPokeNames: Transforms an array of pokemons into an array of pokemon names", () => {
const pokemonNames = getPokeNames();
const pokemonNames = getPokeNames(pokemons);
expect(pokemonNames.length).toBe(151);
expect(pokemonNames[0]).toBe("Bulbasaur");
expect(pokemonNames[pokemonNames.length - 1]).toBe("Mew");
});

test("getPokemonById: Gets a pokemon object by their id", () => {
const id = 25;
const pokemon = getPokemonById(id);
const pokemon = getPokemonById(pokemons, id);
expect(pokemon).toEqual(
expect.objectContaining({
id: expect.any(Number),
Expand All @@ -31,24 +31,24 @@ test("getPokemonById: Gets a pokemon object by their id", () => {
});

test('getRarePokemons: Transforms an array of pokemon into an array of "rare" (spawn_chance is less than 0.1) pokemon', () => {
const rarePokemon = getRarePokemons();
const rarePokemon = getRarePokemons(pokemons);
expect(rarePokemon.length).toBe(81);
expect(rarePokemon.every((pokemon) => pokemon.spawn_chance < 0.1)).toBe(true);
});

test('getMidSizedPokemon: Gets the pokemon that weighs "38.0 kg"', () => {
const pokeMonThatWeighs38kg = getMidSizedPokemon();
const pokeMonThatWeighs38kg = getMidSizedPokemon(pokemons);
expect(pokeMonThatWeighs38kg.name).toBe("Fearow");
});

test("getAdultPokemons: Transforms an array of pokemon into an array of pokemon who cannot be found in eggs", () => {
const adults = getAdultPokemons();
const adults = getAdultPokemons(pokemons);
expect(adults.length).toBe(78);
expect(adults.every((pokemon) => pokemon.egg === "Not in Eggs")).toBe(true);
});

test("getPokemonImages: Transforms an array of pokemon into an array of imageUrls", () => {
const imageUrls = getPokemonImages();
const imageUrls = getPokemonImages(pokemons);
imageUrls.forEach((imageUrl) => {
expect(typeof imageUrl).toBe("string");
});
Expand Down
30 changes: 0 additions & 30 deletions 2.data-mining.js

This file was deleted.

79 changes: 0 additions & 79 deletions 2.data-mining.test.js

This file was deleted.

101 changes: 51 additions & 50 deletions 2.reduce.js
Original file line number Diff line number Diff line change
@@ -1,27 +1,28 @@
const calculateTotalPokemonWeight = (pokemons) => {
return pokemons.reduce((totalWeight, currentPokemon) => {
return totalWeight + parseFloat(currentPokemon.weight)
}, 0)
}
return pokemons.reduce((totalWeight, currentPokemon) => {
return totalWeight + parseFloat(currentPokemon.weight);
}, 0);
};

const calculateAverageSpawnChance = (pokemons) => {
const totalPokemonCount = pokemons.length
const averageSpawnChance = pokemons.reduce((totalSpawnChance, currentPokemon) => {
return totalSpawnChance + currentPokemon.spawn_chance
}, 0) / totalPokemonCount
const totalPokemonCount = pokemons.length;
const averageSpawnChance =
pokemons.reduce((totalSpawnChance, currentPokemon) => {
return totalSpawnChance + currentPokemon.spawn_chance;
}, 0) / totalPokemonCount;

return averageSpawnChance
}
return averageSpawnChance;
};

const calculateTotalEggDistance = (pokemons) => {
return pokemons.reduce((totalDistance, currentPokemon) => {
if (currentPokemon.egg === "Not in Eggs") {
return totalDistance
}
return pokemons.reduce((totalDistance, currentPokemon) => {
if (currentPokemon.egg === "Not in Eggs") {
return totalDistance;
}

return totalDistance + parseInt(currentPokemon.egg)
}, 0)
}
return totalDistance + parseInt(currentPokemon.egg);
}, 0);
};

// Alternate solution using ternary operator
// const calculateTotalEggDistance = (pokemons) => {
Expand All @@ -32,45 +33,45 @@ const calculateTotalEggDistance = (pokemons) => {
// }

const getHeaviestPokemon = (pokemons) => {
return pokemons.reduce((heaviestSoFar, currentPokemon) => {
if (parseInt(heaviestSoFar.weight) > parseInt(currentPokemon.weight)) {
return heaviestSoFar
}
return pokemons.reduce((heaviestSoFar, currentPokemon) => {
if (parseInt(heaviestSoFar.weight) > parseInt(currentPokemon.weight)) {
return heaviestSoFar;
}

return currentPokemon
})
}
return currentPokemon;
});
};

const categorizePokemonsByRarity = (pokemons) => {
const RARE_SPAWN_CHANCE = 0.1
const LEGENDARY_SPAWN_CHANCE = 0.01
const RARE_SPAWN_CHANCE = 0.1;
const LEGENDARY_SPAWN_CHANCE = 0.01;

const initialAccumulator = {
common: [],
rare: [],
legendary: []
}
const initialAccumulator = {
common: [],
rare: [],
legendary: [],
};

return pokemons.reduce((categories, currentPokemon) => {
if (currentPokemon.spawn_chance > RARE_SPAWN_CHANCE) {
categories.common.push(currentPokemon)
return categories
}
return pokemons.reduce((categories, currentPokemon) => {
if (currentPokemon.spawn_chance > RARE_SPAWN_CHANCE) {
categories.common.push(currentPokemon);
return categories;
}

if (currentPokemon.spawn_chance > LEGENDARY_SPAWN_CHANCE) {
categories.rare.push(currentPokemon)
return categories
}
if (currentPokemon.spawn_chance > LEGENDARY_SPAWN_CHANCE) {
categories.rare.push(currentPokemon);
return categories;
}

categories.legendary.push(currentPokemon)
return categories
}, initialAccumulator)
}
categories.legendary.push(currentPokemon);
return categories;
}, initialAccumulator);
};

module.exports = {
calculateTotalPokemonWeight,
calculateAverageSpawnChance,
calculateTotalEggDistance,
getHeaviestPokemon,
categorizePokemonsByRarity
}
calculateTotalPokemonWeight,
calculateAverageSpawnChance,
calculateTotalEggDistance,
getHeaviestPokemon,
categorizePokemonsByRarity,
};
17 changes: 6 additions & 11 deletions 3.reduce.test.js → 2.reduce.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,30 +5,30 @@ const {
calculateTotalEggDistance,
getHeaviestPokemon,
categorizePokemonsByRarity,
} = require("./3.reduce");
} = require("./2.reduce");

test("calculateTotalPokemonWeight: calculates the combined weight of all 151 pokemon", () => {
const totalweight = calculateTotalPokemonWeight();
const totalweight = calculateTotalPokemonWeight(pokemons);
expect(totalweight).toBe(6938.7);
});

test("calculateAverageSpawnChance: calculates the average spawn_chance of a pokemon", () => {
const averageSpawnChance = calculateAverageSpawnChance();
const averageSpawnChance = calculateAverageSpawnChance(pokemons);
expect(averageSpawnChance).toBeCloseTo(0.73, 2);
});

test("calculateTotalEggDistance: calculates how for you have to walk to hatch one of each pokemon egg", () => {
const totalEggDistance = calculateTotalEggDistance();
const totalEggDistance = calculateTotalEggDistance(pokemons);
expect(totalEggDistance).toBe(408);
});

test("getHeaviestPokemon: returns the heaviest pokemon from an array of pokemons", () => {
const heaviestPokemon = getHeaviestPokemon();
const heaviestPokemon = getHeaviestPokemon(pokemons);
expect(heaviestPokemon.id).toBe(143);
});

test("catergorizePokemonsByRarity: catgorizes an array of pokemons based on spawn_chance", () => {
const pokemonsByRarity = categorizePokemonsByRarity();
const pokemonsByRarity = categorizePokemonsByRarity(pokemons);
expect(pokemonsByRarity).toEqual(
expect.objectContaining({
common: expect.any(Array),
Expand All @@ -40,8 +40,3 @@ test("catergorizePokemonsByRarity: catgorizes an array of pokemons based on spaw
expect(pokemonsByRarity.rare.length).toBe(61);
expect(pokemonsByRarity.legendary.length).toBe(24);
});

test("getRarestGym: gets the gym with the most legendary (spawn_chance < 0.01) pokemon", () => {
const rarestGym = getRarestGym();
expect(rarestGym).toEqual({ id: 1, city: "Saffron City", trainerId: 2 });
});
Loading

0 comments on commit 9aa7382

Please sign in to comment.