-
-
Notifications
You must be signed in to change notification settings - Fork 149
feat: add a prototype for the Guild Loot Tracker lab #1078
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
Draft
Sebastian-Wlo
wants to merge
14
commits into
freeCodeCamp:main
Choose a base branch
from
Sebastian-Wlo:lab-nutrient-tracker-dictionary
base: main
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.
Draft
Changes from all commits
Commits
Show all changes
14 commits
Select commit
Hold shift + click to select a range
7da31bd
feat: add prototype for Nutrient Tracker Dictionary lab prototype
Sebastian-Wlo c2ca3e7
feat: add user-stories.md to the nutrient tracker prototype
Sebastian-Wlo fba7edb
chore: renamed "lab-nutrient-tracker-dictionary" directory
Sebastian-Wlo 492458a
docs: change "nutrient tracker" references in lab's "user-stories.md"
Sebastian-Wlo d790e41
refactor: rewrite of "nutrient tracker" functions
Sebastian-Wlo d510dcb
docs: changed the lab's summary section to be more descriptive
Sebastian-Wlo b59a232
refactor: change "listTopMembers" function to "listMembers"
Sebastian-Wlo 789a19f
docs: remove the "listTopMembers" mentions from "user-stories.md", ad…
Sebastian-Wlo 615c8a5
refactor: removed users stories for Guild Loot Tracker
Sebastian-Wlo c1777d2
refactor: restructured Guild Loot Tracker prototype
Sebastian-Wlo 686091e
chore: remove the "lab-" prefix from the prototype's directory name
Sebastian-Wlo 3b7695f
feat: add listMemberNames() function that introduces Object.keys() me…
Sebastian-Wlo cb705ae
refactor: rewrite getMemberTotal() to return an object or `false`
Sebastian-Wlo 77284da
refactor: rewrite 'listMembers', 'getMemberTotals', and 'addLootEntry…
Sebastian-Wlo 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
157 changes: 157 additions & 0 deletions
157
fullstack-cert/js-projects/guild-loot-tracker/script.js
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,157 @@ | ||
| // This will be a workshop for the Loops section, probably after the "Build a Profile Lookup" lab. | ||
|
|
||
| // Creating the "guild" object could be 2 or 3 Steps (creating "guild" object itself and adding the first "guild member" object as the first property - rest of the "guild members" can be filled out somewhere between the next steps to avoid too much repetition). | ||
| let guild = { | ||
| nemo: { | ||
| gold: 31, | ||
| silver: 48, | ||
| reputation: 9, | ||
| experience: 198, | ||
| }, | ||
| shamin: { | ||
| gold: 78, | ||
| silver: 64, | ||
| reputation: 12, | ||
| experience: 111, | ||
| }, | ||
| ahlerich: { | ||
| gold: 41, | ||
| silver: 7, | ||
| reputation: 7, | ||
| experience: 70, | ||
| }, | ||
| corlandus: { | ||
| gold: 81, | ||
| silver: 2, | ||
| reputation: 20, | ||
| experience: 220, | ||
| }, | ||
| pedro: { | ||
| gold: 34, | ||
| silver: 28, | ||
| reputation: 10, | ||
| experience: 179, | ||
| }, | ||
| morgat: { | ||
| gold: 36, | ||
| silver: 81, | ||
| reputation: 12, | ||
| experience: 82, | ||
| }, | ||
| }; | ||
|
|
||
| /* NOTE: | ||
| This method seemed like a good addition before I refactored `listMembers()` - it still might be a good(?), isolated example of `Object.keys()` use, but otherwise seems slightly redundant. | ||
| */ | ||
| // 'listMemberNames()` introduces `Object.keys()` static method, and provides a list of current members of the guild. Could be 3 steps (1. create a function, 2. assign array returned by the `Object.keys()` to a variable + explanation of the method, ) | ||
| function listMemberNames(guildObject) { | ||
| console.log("Current Guild Members:"); | ||
| const memberNames = Object.keys(guildObject); | ||
| for (let i = 0; i < memberNames.length; i++) { | ||
| console.log(`${i + 1}. ${memberNames[i]}`); | ||
| } | ||
| } | ||
|
|
||
| listMemberNames(guild); | ||
|
|
||
| // `listMembers()` introduces `Object.entries()` static method (possibly also `Object.keys()` and `Object.values()`, depending on how long the description for each step is supposed to be) | ||
| function listMembers(guildObject) { | ||
| console.log("Guild Member's Resources:"); | ||
| // TODO: Simplify the loop iteration | ||
| for(const member in guildObject) { | ||
| // Use Object.keys() and Object.resources() to get a list of resources of each guild member | ||
| const resourceNames = Object.keys(guildObject[member]); | ||
| const resourceValues = Object.values(guildObject[member]); | ||
| console.log(`${member}:`); | ||
| console.log(`${resourceNames[0]}\t${resourceNames[1]}\t${resourceNames[2]}\t${resourceNames[3]}`) | ||
| console.log(`${resourceValues[0]}\t${resourceValues[1]}\t${resourceValues[2]}\t\t${resourceValues[3]}`) | ||
| } | ||
| } | ||
|
|
||
| listMembers(guild); | ||
|
|
||
| //TODO: bring the listTopMembers method back (now that the workshop is in the Loops section, sorting should be viable. In Theory. need to double-check that.) | ||
|
|
||
| // 'getMemberTotals()` can be 4 steps (introduce `Object.keys()` method, write a guard clause using `if()/else` and `.includes()`, introduce `Object.values()`, return a string literal with the result) - | ||
| function getMemberTotals(guildObject, member) { | ||
| const memberNames = Object.keys(guildObject); | ||
| // TODO: Simplify the loop iteration | ||
| for (const name in memberNames) { | ||
| if (memberNames[name] === member) { | ||
| return guildObject[member]; | ||
| } | ||
| } | ||
| return false; | ||
| } | ||
| // Checks if `getMemberTotals()` works correctly - can be one of the steps and removed later | ||
| console.log(getMemberTotals(guild, "morgat")); | ||
| console.log(getMemberTotals(guild, "francis")); | ||
|
|
||
| // "cloneGuildData" can be broken into multiple (3?) steps to show that using the spread syntax creates a shallow copy of the object, for example #3 write the function and log it #4 assign the returned data to a variable, and change some of its values, then log both original and the copy to the console, #5 remove the variable and console.log() calls to continue | ||
| function cloneGuildData(guildObject) { | ||
| return { ...guildObject }; | ||
| } | ||
|
|
||
| function addLootEntry(guildObject, entry) { | ||
| // Use cloneGuildData() to prevent accidental mutations before committing the changes to the 'guild object' | ||
| const clonedGuild = cloneGuildData(guildObject); | ||
| // Use `Object.keys() to get the guild member's | ||
| const guildMemberNames = Object.keys(guildObject); | ||
| // Use spread operator to get the member's name and resources out of the "entry" argument | ||
| const { member: memberName, ...newMemberTotals} = entry; | ||
| // Check if the member's name exists, and is a string | ||
| if (!memberName) { | ||
| console.log("New entry must include a name of the guild member!"); | ||
| return; | ||
| } else if (typeof memberName !== "string") { | ||
| console.log("Member name must be a string!"); | ||
| return; | ||
| } | ||
| // Check if the guildObject includes a record of the provided member | ||
| for (let i = 0; i < guildMemberNames.length; i++) { | ||
| if (guildMemberNames[i] === memberName) { | ||
| // Now, we assign the member from the cloned data to a variable to make the loops more readable (we're not making a copy of the object! it could be worth pointing out the difference in the steps). | ||
| const currentMemberTotals = getMemberTotals(clonedGuild, memberName); | ||
| // We could check if the entry includes all of the required fields (the same as the existing member) | ||
| // First we check if the new entry has all of the required resources: | ||
| for (const resource in currentMemberTotals) { | ||
| // TODO: replace `Object.hasOwn() with a different method (going over the Object.keys() for example) | ||
| if (!Object.hasOwn(newMemberTotals, resource)) { | ||
| console.log(`Updated data is missing an entry for "${resource}"!`); | ||
| return; | ||
| } else if ( typeof newMemberTotals[resource] !== typeof currentMemberTotals[resource]){ | ||
| console.log(`"${resource}" must be a "${typeof currentMemberTotals[resource]}" variable!`); | ||
| return; | ||
| } | ||
| // Assigning new values could be done outside of the loop, but on the other hand, it might be how we can show how cloning the current data can prevent against accidental mutation on case where someone provides incorrect input data more clearly (maybe?). | ||
| currentMemberTotals[resource] = newMemberTotals[resource]; | ||
| } | ||
| // Then, we check if the new entry has ONLY the required resources NOTE: (this one is probably unnecessary, I'm not sure if ALL of the resources should actually be required to make an update): | ||
| // TODO: Simplify the loop iteration | ||
| for (const resource in currentMemberTotals) { | ||
| if (!Object.hasOwn(currentMemberTotals, resource)) { | ||
| console.log(`Updated data has an unknown entry "${resource}!`); | ||
| return; | ||
| } | ||
| } | ||
| // Finally, now we're sure our input was correct, we can overwrite the old data. | ||
| guild = clonedGuild; | ||
| console.log("Guild roster updated."); | ||
| return; | ||
| } | ||
| } | ||
| // In the case that the guildObject doesn't include a member with the given name, inform the user and return | ||
| console.log(`${memberName} is not a member of this guild!`); | ||
| return; | ||
| } | ||
|
|
||
| addLootEntry(guild, { | ||
| member: "morgat", | ||
| gold: 1, | ||
| silver: 2, | ||
| reputation: 3, | ||
| experience: 4, | ||
| }); | ||
|
|
||
| // Using `listMembers()` to check if the data has been updated | ||
| listMembers(guild); | ||
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
these names should work