Skip to content

Commit 36888c7

Browse files
committed
string problem solving init
1 parent 9ef7e50 commit 36888c7

2 files changed

Lines changed: 62 additions & 12 deletions

File tree

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
/**
2+
* make query string from an object
3+
* 1st case
4+
* input: {
5+
foo: 'hello',
6+
bar: 'world',
7+
baz: 'true'
8+
}
9+
* output: '?foo=hello&bar=world&baz'
10+
*/
11+
12+
const makeQueryString = (obj) => {
13+
const queries = [];
14+
15+
// check value if true then concate only key else concate key & value.
16+
const insertStr = (key, value) => {
17+
queries.push(`${key}${value === "true" ? "" : "=" + value}`);
18+
};
19+
console.log(1, Object.entries(obj));
20+
21+
for (let [key, value] of Object.entries(obj)) {
22+
// if value is string then
23+
if (typeof value === "string") value = [value];
24+
25+
for (let item of value) {
26+
insertStr(key, item);
27+
}
28+
}
29+
30+
return `?${queries.join("&")}`;
31+
};
32+
33+
// console.log(
34+
// makeQueryString({
35+
// foo: "hello",
36+
// bar: "world",
37+
// baz: "true",
38+
// })
39+
// );
40+
41+
console.log(
42+
makeQueryString({
43+
foo: ["hello", "earth"],
44+
bar: "world",
45+
baz: "true",
46+
})
47+
);
48+
49+
console.log(makeQueryString({}));
50+
51+
// encodeURIComponent()
52+
// decodeURIComponent()

Interview-Questions/queryString.js

Lines changed: 10 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -35,12 +35,10 @@ const formatQueryString = (str) => {
3535

3636
// iterate through key value pairs, split on = , and save in res object as key value
3737
for (const pair of query) {
38-
let [key, value] = pair.split("=");
38+
let [key, value= 'true'] = pair.split("="); // checking value is truthy or undefined else befault 'true' value (3rd case)
3939

4040
// check if key truthy else send empty object (2nd case)
4141
if (key) {
42-
value = value || "true"; // checking value is truthy or undefined else befault 'true' value (3rd case)
43-
4442
// if there is value with the same key, have to save values as array item (4th case)
4543
resObj[key] = resObj[key]
4644
? typeof resObj[key] === "string"
@@ -59,18 +57,18 @@ const formatQueryString = (str) => {
5957
return resObj;
6058
};
6159

62-
// console.log("1st case: ", formatQueryString("?foo=hello&bar=world"), {
63-
// foo: 'hello',
64-
// bar: 'world'
65-
// });
60+
console.log("1st case: ", formatQueryString("?foo=hello&bar=world"), {
61+
foo: 'hello',
62+
bar: 'world'
63+
});
6664
// console.log("2nd case: ", formatQueryString("?"), {}); // if there is no query after ?
6765
// console.log("3rd case: ", formatQueryString("?foo=hello&bar=world&baz"), {
6866
// foo: "hello",
6967
// bar: "world",
7068
// baz: "true",
7169
// });
7270

73-
/*console.log(
71+
console.log(
7472
"4th case: ",
7573
formatQueryString("?foo=hello&bar=world&baz&foo=again&foo=ok&bar=again&baz&dummy&data=earth"),
7674
{
@@ -80,9 +78,9 @@ const formatQueryString = (str) => {
8078
dummy: 'true',
8179
data: 'earth'
8280
}
83-
); */
81+
);
8482

8583
// test case
86-
console.log("4th case: ", formatQueryString("?foo=hello&foo"), {
87-
foo: ["hello", "true"],
88-
});
84+
// console.log("4th case: ", formatQueryString("?foo=hello&foo"), {
85+
// foo: ["hello", "true"],
86+
// });

0 commit comments

Comments
 (0)