-
Notifications
You must be signed in to change notification settings - Fork 7
Feature/solutions #8
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
Open
lakylekidd
wants to merge
16
commits into
TechmongersNL:feature/solutions
Choose a base branch
from
lakylekidd:feature/solutions
base: feature/solutions
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
16 commits
Select commit
Hold shift + click to select a range
beb3af6
Merge pull request #1 from Codaisseur/feature/exercises
Reinoptland 3a0a7de
Revert "Remove solution code, add test scripts"
Reinoptland bc509a6
Merge pull request #2 from Codaisseur/revert-1-feature/exercises
Reinoptland cb4de24
Fix merge conflict
Reinoptland 457a425
Improve solution for getTrainerPokemons
Reinoptland ea71846
Added first test
lakylekidd 903730a
Added test and implementation of getPsychicTrainersAndGyms
lakylekidd 250ad51
Included additional expect to test for gym city names
lakylekidd 29df8de
Added new test and solution to concatenate trainers gyms and pokemons
lakylekidd 0288db3
Adjusted getPsychicTrainersAndGyms function to use newly added function
lakylekidd d5b8975
Fixed Celadon City by removing empty space after the city
lakylekidd 93ce5b0
Added pokemon id 44 to Brock trainer to have variety of data for next…
lakylekidd 181eaa5
Included test and solution for getting gyms with certain pokemons
lakylekidd fa185ed
Remove mutation of trainer array
lakylekidd bf5291c
Avoid mutation of trainer array
lakylekidd 044c7d4
Return new object
lakylekidd File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
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
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
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
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
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,72 @@ | ||
| const getTrainersAndGymsAndPokemons = (gyms, trainers, pokemons) => { | ||
| // Combine trainers and pokemons and gyms | ||
| return ( | ||
| trainers | ||
| // Map trainers to include their pokemons | ||
| .map(trainer => { | ||
| // Include the trainer's pokemons | ||
| const trainerPokemons = pokemons.filter(pokemon => | ||
| trainer.pokemonIds.includes(pokemon.id) | ||
| ); | ||
| // Include the trainer's gym | ||
| const trainerGym = gyms.find(gym => gym.trainerId === trainer.id); | ||
| return { | ||
| ...trainer, | ||
| pokemons: trainerPokemons, | ||
| gym: trainerGym | ||
| } | ||
| }) | ||
| ); | ||
| }; | ||
|
|
||
| const getPsychicTrainersAndGyms = (gyms, trainers, pokemons) => { | ||
| return ( | ||
| // Step 1: Combine the trainers, gyms and pokemons | ||
| getTrainersAndGymsAndPokemons(gyms, trainers, pokemons) | ||
| // Step 2: Filter out only trainers that have a psychic pokemon | ||
| .filter(trainer => | ||
| trainer.pokemons.some(pokemon => pokemon.type.includes("Psychic")) | ||
| ) | ||
| ); | ||
| }; | ||
|
|
||
| const getGymsWithPokemons = (gyms, trainers, pokemons, ...pokemonsToFind) => { | ||
| // Retrieve the pokemon objects | ||
| return ( | ||
| pokemons | ||
| // Filter all the pokemons based on the ones we requested to find | ||
| .filter(pokemon => pokemonsToFind.includes(pokemon.name)) | ||
| .map(pokemon => { | ||
| // Construct the object to be returned | ||
| return { | ||
| id: pokemon.id, | ||
| name: pokemon.name, | ||
| // Retrieve the trainers that own this pokemon | ||
| trainers: trainers.filter(trainer => | ||
| trainer.pokemonIds.includes(pokemon.id) | ||
| ), | ||
| // Retrieve the gyms that are owned by these trainers | ||
| gyms: gyms.filter(gym => { | ||
| return trainers | ||
| .filter(trainer => trainer.pokemonIds.includes(pokemon.id)) | ||
| .find(trainer => trainer.id === gym.trainerId); | ||
| }) | ||
| }; | ||
| }) | ||
| ); | ||
|
|
||
| return getTrainersAndGymsAndPokemons(gyms, trainers, pokemons) | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think this is unreachable code ...
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Could be. I was making lots of tests I might have overlooked it. I'll check again. |
||
| .filter(trainer => | ||
| trainer.pokemons.some(pokemon => pokemonsToFind.includes(pokemon.name)) | ||
| ) | ||
| .map(trainer => { | ||
| return {}; | ||
| trainer.gym; | ||
| }); | ||
| }; | ||
|
|
||
| module.exports = { | ||
| getPsychicTrainersAndGyms, | ||
| getTrainersAndGymsAndPokemons, | ||
| getGymsWithPokemons | ||
| }; | ||
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,94 @@ | ||
| const pokemons = require("./pokeData"); | ||
| const trainers = require("./trainerData"); | ||
| const gyms = require("./gymData"); | ||
|
|
||
| const { | ||
| getPsychicTrainersAndGyms, | ||
| getTrainersAndGymsAndPokemons, | ||
| getGymsWithPokemons | ||
| } = require("./4.student-exercises"); | ||
|
|
||
| /** | ||
| * This function should return an array of trainer objects | ||
| * that also include an array of pokemons and an object of gym | ||
| */ | ||
| test("getTrainersAndGymsAndPokemons", () => { | ||
| // Get an array of trainers that contain their gyms and pokemons | ||
| const trainersAndGymsAndPokemons = getTrainersAndGymsAndPokemons( | ||
| gyms, | ||
| trainers, | ||
| pokemons | ||
| ); | ||
| expect(trainersAndGymsAndPokemons.length).toBe(9); | ||
| // Expect each trainer to have a gym object | ||
| expect(trainersAndGymsAndPokemons.map(trainer => trainer.gym)).toEqual( | ||
| expect.any(Object) | ||
| ); | ||
| // Expect each trainer to have a pokemon array | ||
| expect(trainersAndGymsAndPokemons.map(trainer => trainer.pokemons)).toEqual( | ||
| expect.any(Array) | ||
| ); | ||
| }); | ||
|
|
||
| /** | ||
| * This function should return an array of trainers that have | ||
| * at least one psychic pokemon. In each trainer object an array | ||
| * of their own pokemons and an object representing their gym should exist | ||
| */ | ||
| test(`getPsychicTrainersAndGyms: expect a list of trainers with psychic pokemons. | ||
| also include (all) their pokemons and their gym`, () => { | ||
| const result = getPsychicTrainersAndGyms(gyms, trainers, pokemons); | ||
| expect(result.length).toEqual(2); | ||
| expect(result[0].name).toBe("Sabrina"); | ||
| expect(result[1].name).toBe("Misty"); | ||
| // Results should also contain gym | ||
| expect(result[0].gym).toEqual(expect.any(Object)); | ||
| expect(result[1].gym).toEqual(expect.any(Object)); | ||
| expect(result[0].gym.city).toBe("Saffron City"); | ||
| expect(result[1].gym.city).toBe("Cerulean City"); | ||
| // Results should also contain array of pokemons | ||
| expect(result[0].pokemons).toEqual(expect.any(Array)); | ||
| expect(result[1].pokemons).toEqual(expect.any(Array)); | ||
| expect(result[0].pokemons.length).toBe(4); | ||
| expect(result[1].pokemons.length).toBe(2); | ||
| }); | ||
|
|
||
| /** | ||
| * Returns an array of objects with the id and name of pokemon and an array | ||
| * of gym objects where these pokemons can be found. | ||
| */ | ||
| test("getGymsWithPokemons: expect a list of gyms where certain pokemons can be found", () => { | ||
| const result = getGymsWithPokemons( | ||
| gyms, | ||
| trainers, | ||
| pokemons, | ||
| "Gloom", | ||
| "Starmie" | ||
| ); | ||
| // Expect the result to be an array | ||
| result.forEach(pokemon => { | ||
| // It is an array of pokemons | ||
| expect(pokemon).toEqual( | ||
| expect.objectContaining({ | ||
| id: expect.any(Number), | ||
| name: expect.any(String), | ||
| trainers: expect.any(Array), // Should contain a list of trainers | ||
| gyms: expect.any(Array) // Should contain a list of gyms | ||
| }) | ||
| ); | ||
| }); | ||
| // Length of array should be 2 | ||
| expect(result.length).toEqual(2); | ||
| // Check owners | ||
| expect(result[0].trainers.length).toEqual(2); | ||
| expect(result[1].trainers.length).toEqual(1); | ||
| expect(result[0].trainers[0].name).toBe("Brock"); | ||
| expect(result[0].trainers[1].name).toBe("Erika"); | ||
| expect(result[1].trainers[0].name).toBe("Misty"); | ||
| // Check Gyms | ||
| expect(result[0].gyms.length).toEqual(2); | ||
| expect(result[1].gyms.length).toEqual(1); | ||
| expect(result[0].gyms[0].city).toBe("Celadon City"); | ||
| expect(result[0].gyms[1].city).toBe("Pewter City"); | ||
| expect(result[1].gyms[0].city).toBe("Cerulean City"); | ||
| }); |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.