I'm trying to replicate the functionality of JSON.stringify() with several primitive types, so far I have only managed to pass 'number' and 'string' (the easy ones) which in turn make, function and empty string also pass the test.
function stringifier (input) {
if (typeof input === 'number') return `${input}`;
if (typeof input === 'string') return `"${input}"`;
if (Array.isArray(input)) return `[${input.map(stringifier)}]`;
}
Now I'm trying to pass null and undefined but I just can't make it work... The parameter is passed different values to make all the tests pass (array, object, nested arr...).
Im currently using if (typeof input === null) return String(input) which I have tried separately and has worked. Have checked the type and its a string, I just don't understand why is not happening in the exercise. I'd like to have 'null', same as with undefined.
Also I have been trying to implement recursion on the Array with no success if (Array.isArray(input)) return [${input.map(stringifier)}]; (with backticks).
typeof nullis neithernullnot"null", it's"object".typeofnever returns'null'. You probably meanif (input === null)if (input === null)is the right way though. Make sure to get your order right, since it would also match anif (typeof input === 'object')test, it would have to go above such a test. Also, if something doesn't work, instead of just saying "it doesn't pass the test and I don't know why", you could use a debugger to look at the execution step by step and inspect the variables and expressions that get used, to be able to follow what's happening and see why it doesn't work.