AlgoDaily Solution
1function stringify(data) {
2 if (typeof data === "string") {
3 return `"${data}"`;
4 }
5 if (typeof data === "function") {
6 return undefined;
7 }
8 if (data === Infinity || data === -Infinity || data === null
9 || data === undefined || typeof data === "symbol") {
10 return "null";
11 }
12 if (typeof data === "number" || typeof data === "boolean") {
13 return `${data}`;
14 }
15
16 if (data instanceof Date) {
17 return `"${data.toISOString()}"`;
18 }
19 if (Array.isArray(data)) {
20 const arr = data.map((el) => stringify(el));
21 return `[${arr.join(",")}]`;
22 }
23 if (typeof data === "object") {
24 const arr = Object.entries(data).reduce((acc, [key, value]) => {
25 if (value === undefined) {
26 return acc;
27 }
28 acc.push(`"${key}":${stringify(value)}`);
29 return acc;
30 }, []);
31 return `{${arr.join(",")}}`;
32 }
33}
Community Solutions
Community solutions are only available for premium users.
Access all course materials today
The rest of this tutorial's contents are only available for premium members. Please explore your options at the link below.
xxxxxxxxxx
34
function stringify(data) {
if (typeof data === "string") {
return `"${data}"`;
}
if (typeof data === "function") {
return undefined;
}
if (data === Infinity || data === -Infinity || data === null
|| data === undefined || typeof data === "symbol") {
return "null";
}
if (typeof data === "number" || typeof data === "boolean") {
return `${data}`;
}
if (data instanceof Date) {
return `"${data.toISOString()}"`;
}
if (Array.isArray(data)) {
const arr = data.map((el) => stringify(el));
return `[${arr.join(",")}]`;
}
if (typeof data === "object") {
const arr = Object.entries(data).reduce((acc, [key, value]) => {
if (value === undefined) {
return acc;
}
OUTPUT
Results will appear here.