FrontendArk

Master Frontend Interviews

  • Practice
  • Playground
  • Resource
Medium

JSON.stringify

20mins

Implement a function jsonStringify(value) that converts a JavaScript value into a JSON string.
Only the following types will appear in input: boolean, number, null, string, array, or object.

Requirements:

  • Ignore optional parameters from the original JSON.stringify.
  • Arrays and objects should be recursively serialized.
  • Strings should be wrapped in double quotes.
  • Boolean, null, and numbers should be converted as in JSON.

Examples

jsonStringify({ foo: 'bar' }); // '{"foo":"bar"}' jsonStringify({ foo: 'bar', bar: [1, 2, 3] }); // '{"foo":"bar","bar":[1,2,3]}' jsonStringify({ foo: true, bar: false }); // '{"foo":true,"bar":false}' jsonStringify(null); // 'null' jsonStringify(true); // 'true' jsonStringify(false); // 'false' jsonStringify(1); // '1' jsonStringify('foo'); // '"foo"'

Code Editor

JavaScript Function

00:00
Loading...

Test Cases

No test results yet

Click "Run" to execute tests