Repository navigation
Conversation
… the test.test passed
| let [key, ...rest] = pair.split("="); | ||
| let value = rest.join("="); | ||
|
|
||
| const lastThreeCharKey = key.slice(-3); |
There was a problem hiding this comment.
is there reason for slicing the last 3 letter word?
are we certain that the key will always contain a 3 character second word?
what if we pass the following input the fn?
parseQueryString("name=John+Doe+Foo&age=30&city=New+York")
is the output correct?
There was a problem hiding this comment.
I have changed it to -2, and why I chose to that because I wanted to differentiate between an equation that hasn't been decoded and a word.
The out put was not correct. Only the first "+" sign was changed. so I made the changes to replace all .
|
|
||
| const lastThreeCharKey = key.slice(-3); | ||
| if (!lastThreeCharKey.includes("+") || key.includes("%")) { | ||
| key = decodeURIComponent(key.replace("+", " ")); |
There was a problem hiding this comment.
using a replace will only replace the first occurrence of "+", what if there are more than one occurrences.
is there another fn that replaces all the occurrences of a char?
|
|
||
| const lastThreeCharKey = key.slice(-2); | ||
| if (!lastThreeCharKey.includes("+") || key.includes("%")) { | ||
| key = decodeURIComponent(key.replaceAll("+", " ")); |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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).
before I edited the code I used last three to check for equations with an ending of two characters lets say , 2x + 4x. Can you please explain more about what you want me to do here. I need guidance. Thank you .
There was a problem hiding this comment.
Let's take the example of the following input:
a+b+c=x+y+zWhat your code does right now
pair = "a+b+c=x+y+z" line 14 key = "a+b+c" line 15 value = "x+y+z" line 17 lastThreeCharKey = key.slice(-2) → "+c" line 18 !"+c".includes("+") → !true → false key.includes("%") → false false || false → false ← branch skipped line 19 never runs. key stays "a+b+c" line 21 lastThreeCharVal = value.slice(-2) → "+z" line 22 !"+z".includes("+") → !true → false ← branch skipped line 23 never runs. value stays "x+y+z" line 25 queryParams["a+b+c"] = "x+y+z" returns { "a+b+c": "x+y+z" }Your
replaceAllon lines 19 and 23 is correct — it just never executes, because theifabove it is false. Compare withname=John+Doe, where"John+Doe".slice(-2)is"oe", the condition is true, and you get{name: "John Doe"}. Same kind of input, opposite result, decided purely by which characters happen to sit at the end.What the answer should be
new URLSearchParams("a+b+c=x+y+z") // → { "a b c": "x y z" } // this is the built-in fn that we're trying to build in this exercise.In a query string
+always means a space — there's no maths-equation exception. A literal plus travels as%2B. In facta+b+c=x+y+zis exactly what a browser produces when you encode the keya b cwith the valuex y z:const p = new URLSearchParams(); p.set("a b c", "x y z"); p.toString(); // "a+b+c=x+y+z"What I'm suggesting
Delete lines 17, 18, 20, 21, 22 and 24 — both slices, both
ifs and the%clause. Keep the two lines you already wrote, running unconditionally:line 14 key = "a+b+c" line 15 value = "x+y+z" key = decodeURIComponent("a+b+c".replaceAll("+", " ")) → "a b c" → decode → "a b c" value = decodeURIComponent("x+y+z".replaceAll("+", " ")) → "x y z" → decode → "x y z" queryParams["a b c"] = "x y z"And the escaped case still survives, because
replaceAllruns before decoding, while%2Bis still three plain characters:"2x%2B4x".replaceAll("+", " ") → "2x%2B4x" (no "+" to find) decodeURIComponent("2x%2B4x") → "2x+4x" ✅That's the whole reason no detection is needed: replace first, decode second, and the two cases can never collide.
Hope this helps. Thanks
There was a problem hiding this comment.
Thank you very much!! In deed it has. I have made all the necessary changes and all the test pass.
| const lastThreeCharKey = key.slice(-2); | ||
| if (!lastThreeCharKey.includes("+") || key.includes("%")) { | ||
| key = decodeURIComponent(key.replaceAll("+", " ")); | ||
| } | ||
| const lastThreeCharVal = value.slice(-2); | ||
| if (!lastThreeCharVal.includes("+")) { | ||
| value = decodeURIComponent(value.replaceAll("+", " ")); |
There was a problem hiding this comment.
What happens to a key or value ending in + when its last two characters are checked here, for example in name=Mary+?
To "like" or "dislike" this comment, please follow this link
| function tally(arrayItems) { | ||
| const countItem = {}; | ||
| if (!Array.isArray(arrayItems)) { | ||
| throw new Error("tally rquires an input"); |
There was a problem hiding this comment.
How does this error message compare with the invalid-input contract in the test file, and what message do the new tests require?
To "like" or "dislike" this comment, please follow this link
| // My prediction is that ingredients are not gonna be printed out. because the object | ||
| // is not attached to the key in the console.log. |
There was a problem hiding this comment.
What value does ${recipe} interpolate in the original log statement, and how does that compare with ${recipe.ingredients}?
To "like" or "dislike" this comment, please follow this link
There was a problem hiding this comment.
the first one will display "[object,object]" when interpolating an object. And when interpolating an array it will log out ingredients list as a string as well
|
The CYF AI review has left comments. It will only review a PR one time. When you have addressed these comments, please request review again and a volunteer will take a look. |
Learners, PR Template
Self checklist
Task code
CYF-1083
Changelist
I have done all the course work and created new file for the Invert.js ans have pushed it along with the rest of file
Questions