Normalize Mixed to Strings
JavaScript
Medium
4 views
Problem Description
You get a JSON array. Convert each item to a display string: primitives use String(x), null becomes 'null', objects/arrays become JSON string. Print JSON array of these strings.
Input Format
One line JSON array.
Output Format
One line JSON array of strings.
Sample Test Case
Output:
["1","true","null","[2]"]
Constraints
Array length up to 1e5.
Official Solution
const fs=require('fs');const s=fs.readFileSync(0,'utf8').trim();if(!s)process.exit(0);const arr=JSON.parse(s);const out=[];for(const v of arr){if(v===null)out.push('null');else if(typeof v==='object')out.push(JSON.stringify(v));else out.push(String(v));}process.stdout.write(JSON.stringify(out));
Solutions (0)
No solutions submitted yet. Be the first!
No comments yet. Start the discussion!