Mark As Completed Discussion

Good afternoon! Here's our prompt for today.

In Javascript there is a method called JSON.stringify() that can convert an object or a variable into a JSON string. The method handles different data types differently, ending up with its correct JSON "translation". Some examples of how this method handles different types include:

  • Boolean, Number, and String objects are converted to the corresponding primitive values
  • undefined, Function, and Symbol are not valid JSON values and are changed to null
  • The instances of Date are returned as date.toISOString() and are treated as strings
  • Infinity as well as the value null are considered as null

Can you implement a simplified version of this JSON stringifier, covering the most important constraints when it comes to the data type and value handling?

Here is an example of how your solution should work:

JAVASCRIPT
1stringify({});                    // '{}'
2stringify(true);                  // 'true'
3stringify('foo');                 // '"foo"'
4stringify([1, 'false', false]);   // '[1,"false",false]'
5stringify([null, Infinity]); 	  // '[null,null]'
6stringify({ x: 5 });              // '{"x":5}'

Question

Try to solve this here or in Interactive Mode.

How do I practice this challenge?

JAVASCRIPT
OUTPUT
:001 > Cmd/Ctrl-Enter to run, Cmd/Ctrl-/ to comment

Here's our guided, illustrated walk-through.

How do I use this guide?