function stringifyJSON(obj) {
// your code goes here
if(typeof obj === 'number'){
return String(obj);
} else if(obj === null ){
return `${obj}`
}else if(typeof obj === 'string'){
return `"${obj}"`
}
let result = ""
if(Array.isArray(obj)){
result += "[";
for(let el of obj){
result += `${stringifyJSON(el)},`;
}
if(result === "["){
return result+"]"
}else{
return result = result.slice(0, result.length-1) + "]"
}
}else if(typeof obj === "object"){
result += "{"
for(let key in obj){
if(obj[key] === undefined || typeof obj[key] === 'function'){
continue;
}
result += `"${key}":`
result += `${stringifyJSON(obj[key])},`;
}
if(result === "{"){
return result+"}"
}else{
return result = result.slice(0, result.length-1) + "}"
}
}return String(obj);
};
'Algorithm > 코플릿' 카테고리의 다른 글
set.has (0) | 2023.04.20 |
---|---|
Daily Coding 22(메모이제이션) (2) | 2023.04.14 |
Daily Coding 18 (0) | 2023.04.07 |
Server과제 (0) | 2023.04.06 |
Daily Coding 17 (0) | 2023.04.06 |