London | 25-ITP-SEP | Imran Mohamed | Sprint 2 | Sprint-2 exercises#809
London | 25-ITP-SEP | Imran Mohamed | Sprint 2 | Sprint-2 exercises#809i786m wants to merge 15 commits intoCodeYourFuture:mainfrom
Conversation
Sprint-2/debug/recipe.js
Outdated
| console.log(`${recipe.title} serves ${recipe.serves} | ||
| ingredients: | ||
| ${recipe}`); | ||
| ${recipe.ingredients}`); |
There was a problem hiding this comment.
This change does not yet satisfy the requirement written on line 6.
Sprint-2/implement/lookup.js
Outdated
| if (Array.isArray(currency)) { | ||
| result[country] = currency; | ||
| } | ||
| if (currency === null || currency === undefined) { | ||
| continue; | ||
| } | ||
|
|
||
| if (country.length && currency.length && typeof country === 'string' && typeof currency === 'string') { | ||
| result[country] ? result[country] = [result[country], currency] | ||
| : result[country] = currency; | ||
| } |
There was a problem hiding this comment.
Why check country.length and type of country on line 20 but not on line 13?
Why check result[country] on line 21 but not on line 14?
There was a problem hiding this comment.
Amended to include checks, and destructure country and currency
Sprint-2/implement/querystring.js
Outdated
| if(pair.indexOf('=') !== pair.lastIndexOf('=')){ | ||
| const firstEqualIndex = pair.indexOf('='); | ||
| const key = pair.substring(0, firstEqualIndex); | ||
| const value = pair.substring(firstEqualIndex + 1); | ||
| queryParams[key] = value; | ||
| continue; | ||
| } else { | ||
| const [key, value] = pair.split("="); | ||
| queryParams[key] = value; | ||
| } |
There was a problem hiding this comment.
Please note that in real querystring, both key and value are percent-encoded or URL encoded in the URL. For example, the string "5%" will be encoded as "5%25". So to get the actual value of "5%25" (whether it is a key or value in the querystring), you should call a function to decode it.
May I suggest looking up any of these terms, and "How to decode URL encoded string in JS"?
There was a problem hiding this comment.
Implemented decoding by calling decodeURIComponent on the string passed
Sprint-2/implement/tally.js
Outdated
| const result = {}; | ||
| for (const item of arr) { | ||
| result[item] = (result[item] || 0) + 1; | ||
| } |
There was a problem hiding this comment.
Does the following function call returns the value you expect?
tally(["toString", "toString"]);
Suggestion: Look up an approach to create an empty object with no inherited properties.
There was a problem hiding this comment.
Implemented Object.create(null) to create an empty object with no inherited properties and test for strings with object methods to confirm there is no unwanted behaviour
Sprint-2/implement/tally.test.js
Outdated
| test("should handle array with null and undefined values", () => { | ||
| expect(tally([null, undefined, null, 'a', undefined])).toEqual({ null: 2, undefined: 2, a: 1 }); | ||
| }); |
There was a problem hiding this comment.
What do you expect from the following function call?
tally([null, undefined, null, 'a', undefined, "null", "undefined"]
There was a problem hiding this comment.
Implemented test and check for undefined/null vs strings of null/undefined by allocating a separate property for null and undefined values
…ayed on a new line
…urrency; improve readability
…mprove handling of encoded characters in tests
…te keys; update tests for clarity and edge cases
Sprint-2/implement/lookup.js
Outdated
| if (Array.isArray(currency) && typeof country === "string") { | ||
| result[country] | ||
| ? (result[country] = [result[country], currency]) | ||
| : (result[country] = currency); | ||
| } | ||
|
|
||
| if (country.length && currency.length && typeof country === 'string' && typeof currency === 'string') { | ||
| result[country] | ||
| ? (result[country] = [result[country], currency]) | ||
| : (result[country] = currency); | ||
| } |
There was a problem hiding this comment.
-
Can you explain why the condition on line 25 needs to check if
country.lengthis non-zero, but the condition on line 19 does not? -
The normal use of the
? :operator is,condition ? expr1 : expr2, to conditionally evaluates to one of the expressions.
Lines 20-22 is typically expressed as:
result[country] = result[country] ? [result[country], currency] : currency;
There was a problem hiding this comment.
I have combined both of the conditions so that the checks apply to both strings and arrays for currency, and have refactored the ternary as suggested.
…mprove readability and maintainability
…and key-value pairs; Implement decoding uri component at key value pair level; update tests for consistency with expected string outputs
…types; update tests for consistency with new key format
| test('should return an object', () => { | ||
| expect(typeof tally([])).toBe('object'); | ||
| }) | ||
| // Given an empty array | ||
| // When passed to tally | ||
| // Then it should return an empty object | ||
| test.todo("tally on an empty array returns an empty object"); | ||
| test("should return an empty object when given an empty array", () => { | ||
| expect(tally([])).toEqual({}); | ||
| }); |
There was a problem hiding this comment.
Is the test on 22-24 necessary?
| if (((Array.isArray(currency) && currency.length)|| (typeof currency === 'string' && currency.length)) | ||
| && typeof country === "string" && country.length) { | ||
| result[country] = result[country] ? [result[country], currency] : currency; | ||
| } |
There was a problem hiding this comment.
Suppose before line 19,
- value of
countryis "A" - value of
result[country]is"A1" - value of
currencyis["A2", "A3"]
What is your expected value of result[country] after line 22?
Learners, PR Template
Self checklist
Changelist
Completed the tasks set out in the debug, implement and interpret folders in sprint 2