Implement JSON.stringify
HardObjects30 min5 tests
Implement a simplified version of JSON.stringify that handles common JavaScript types.
Requirements
- Handle: strings, numbers, booleans, null, arrays, and plain objects
- Strings should be wrapped in double quotes
undefinedvalues in objects should be omitted- Nested objects and arrays should be handled recursively
Example
jsonStringify({ a: 1, b: "hello", c: [true, null] });
// '{"a":1,"b":"hello","c":[true,null]}'Hints (4)
- Handle each type with if/else: null, string, number, boolean, undefined.
- For arrays, recursively stringify each element. Replace undefined with null.
- For objects, iterate keys, skip undefined values, and format as
"key":value. - Strings need to be wrapped in double quotes.
Topics
- Objects
Asked at
Google · Meta · Amazon · Uber
