Repository navigation
London | 26-ITP-Sep | Maharit Belai | Sprint 2 | coursework/sprint-2 #1512
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
base: main
Are you sure you want to change the base?
Changes from all commits
9b10ba6
586987d
9dbc72a
59f4ca7
759cd2f
5a26afc
fc407f2
ec28696
b1637b2
d14c2b1
f48a0d2
0fd541f
5d3bf95
6ac330d
10992d0
9e03adc
74bd5bc
409fe5a
5623be9
6101574
a117269
5cb8310
7b4ff8f
13ec1b1
c91022e
b36b46c
cbf65e0
0c1d9b3
bb13213
d399dd1
af3f0eb
9907a22
2aac822
f0eefc8
c04e5e6
c250694
43f3f65
5249da4
a96ef2d
f163306
276e30c
9c2fa1d
4513817
a05704c
5d5fb7e
e03d59d
cc041a1
3afcdc4
f162460
1b17717
a7b8c0d
a6c4e31
7828f7b
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,15 +1,21 @@ | ||
| // Predict and explain first... | ||
| // My prediction is that ingredients are not gonna be printed out. because the object | ||
| // is not attached to the key in the console.log. | ||
|
|
||
| // This program should log out the title, how many it serves and the ingredients. | ||
| // Each ingredient should be logged on a new line | ||
| // How can you fix it? | ||
|
|
||
| // We can fix it by using for..of loop for the array . | ||
|
|
||
| const recipe = { | ||
| title: "bruschetta", | ||
| serves: 2, | ||
| ingredients: ["olive oil", "tomatoes", "salt", "pepper"], | ||
| }; | ||
|
|
||
| console.log(`${recipe.title} serves ${recipe.serves} | ||
| ingredients: | ||
| ${recipe}`); | ||
| console.log(`${recipe.title} serves ${recipe.serves}`); | ||
|
|
||
| for (const item of recipe.ingredients) { | ||
| console.log(item); | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,3 +1,14 @@ | ||
| function contains() {} | ||
| function contains(objIn, property) { | ||
| if (Array.isArray(objIn) || objIn === null || typeof objIn !== "object") { | ||
| throw new Error("contains requires an object"); | ||
| } | ||
|
|
||
| for (const key in objIn) { | ||
| if (key === property) { | ||
| return true; | ||
| } | ||
| } | ||
| return false; | ||
| } | ||
|
|
||
| module.exports = contains; |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,5 +1,10 @@ | ||
| function createLookup() { | ||
| // implementation here | ||
| function createLookup(array) { | ||
| const countryCurrency = {}; | ||
|
|
||
| for (let i = 0; i < array.length; i++) { | ||
| countryCurrency[array[i][0]] = array[i][1]; | ||
| } | ||
| return countryCurrency; | ||
| } | ||
|
|
||
| module.exports = createLookup; |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,15 +1,29 @@ | ||
| function parseQueryString(queryString) { | ||
|
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. what happens when you pass the following input to the fn? parseQueryString("a+b+c=x+y+z") is this the correct output? |
||
| const queryParams = {}; | ||
|
|
||
| if (queryString.length === 0) { | ||
| return queryParams; | ||
| } | ||
|
|
||
| const keyValuePairs = queryString.split("&"); | ||
|
|
||
| for (const pair of keyValuePairs) { | ||
| const [key, value] = pair.split("="); | ||
| if (!pair) { | ||
| continue; | ||
| } | ||
| let [key, ...rest] = pair.split("="); | ||
| let value = rest.join("="); | ||
|
|
||
| const lastThreeCharKey = key.slice(-2); | ||
| if (!lastThreeCharKey.includes("+") || key.includes("%")) { | ||
| key = decodeURIComponent(key.replaceAll("+", " ")); | ||
|
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. you're on the right track using replaceAll fn, however there is no need to detect if the key or value contains a "+". Since replaceAll replaces all the instances of "+" we don't need to worry about how many times it appears in the string.
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. what I am doing right on that if comparison is that I am checking the last two characters, fore example if the key value has an equation like in one of my tests.(a+b+c=x+y+z) so it will check if the last characters include plus sign and then replace it if it is not equation**( first+name, last+name)**. I am not counting rather I am checking. (There is no other way to change +sign even if I decode it). |
||
| } | ||
| const lastThreeCharVal = value.slice(-2); | ||
| if (!lastThreeCharVal.includes("+")) { | ||
| value = decodeURIComponent(value.replaceAll("+", " ")); | ||
| } | ||
| queryParams[key] = value; | ||
| } | ||
|
|
||
| return queryParams; | ||
| } | ||
|
|
||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,3 +1,15 @@ | ||
| function tally() {} | ||
|
|
||
| function tally(arrayItems) { | ||
| const countItem = {}; | ||
| if (!Array.isArray(arrayItems)) { | ||
| throw new Error("tally rquires an input"); | ||
| } | ||
| for (const item of arrayItems) { | ||
| if (countItem[item]) { | ||
| countItem[item] += 1; | ||
| } else { | ||
| countItem[item] = 1; | ||
| } | ||
| } | ||
| return countItem; | ||
| } | ||
| module.exports = tally; |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,14 @@ | ||
| const invert = require("./invert.js"); | ||
| // for an empty object | ||
| test(" should return an empty object if given empty object", () => { | ||
| expect(invert({})).toEqual({}); | ||
| }); | ||
| //test for one proprty in an object | ||
| test(" should return an object inverted when given one property object", () => { | ||
| expect(invert({ a: 1 })).toEqual({ 1: "a" }); | ||
| }); | ||
| //test for two property in an object | ||
| test(" should return an object that is inverted when given two property object", () => { | ||
| expect(invert({ a: 1, b: 2, c: 3 })).toEqual({ 1: "a", 2: "b", 3: "c" }); | ||
| }); | ||
| // |
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.
Its true that author is not iterable.
however the reason for that isn't that it uses keys instead. More on this in the following article.
https://medium.com/@mohammedelaouri/understanding-why-objects-are-not-iterable-in-javascript-and-the-best-ways-to-iterate-over-them-5048f8edf5d8
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.
Thank you for the article! It is insightful.