Algorithm/코플릿
JSON.stringify 와 재귀를 이용한 과제
Summer.dev
2023. 4. 12. 16:22
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);
};