// Asked in Thoughtspot, Multiplier
// Write custom function for JSON.stringify
// addr: ["chandpol", "avv"],
// myUndefined: undefined,
// function myStringify( your args) {
// "addr":["chandpol","avv"],
// "nested":{"name":"Nested","valid":true}
// In interview don't handle all data type in one shot, you can start with
// string number object array then go for handling circular depedency
// handle null properly, beacause typeof null === object
function myStringify(value, seen = new WeakSet()) {
if (value === null || value === undefined || typeof value === "symbol") {
if (typeof value === "string") {
if (typeof value === "number" || typeof value === "boolean") {
if (typeof value === "function") {
if (Array.isArray(value)) {
myStringify(item, seen) === undefined ? "null" : myStringify(item, seen)
return `[${arrayResult}]`;
if (typeof value === "object") {
throw new Error("Circular reference detected");
let objResult = Object.entries(value)
.filter(([key, val]) => typeof val !== "function" && val !== undefined)
.map(([key, val]) => `"${key}":${myStringify(val, seen)}`)
throw new Error(`Unsupported data type: ${typeof value}`);
console.log(myStringify(obj));
// What if there is circular dependency ?
const output = myStringify(obj);
console.error(error.message);