2

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).

16
  • 2
    by json_encode you mean stringifier itself? Commented Oct 10, 2022 at 21:35
  • 1
    typeof null is neither null not "null", it's "object". Commented Oct 10, 2022 at 21:42
  • 3
    @KonradLinkowski typeof never returns 'null'. You probably mean if (input === null) Commented Oct 10, 2022 at 21:43
  • 1
    if (input === null) is the right way though. Make sure to get your order right, since it would also match an if (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. Commented Oct 11, 2022 at 7:07
  • 1
    The recursion also looks correct, by the way. Again, "with no success" is not so helpful there, you should trace the execution in a debugger and see what's broken about it. Commented Oct 11, 2022 at 7:09

1 Answer 1

1

Here is the updated function, most test do work on a different file than the provided by the school.

function stringifier(input) {

  if (typeof input === 'function') return undefined;
  if (typeof input === 'number' || typeof input === 'boolean') return `${input}`;
  if (typeof input === 'string' || input === '') return `"${input}"`;
  if (input === null) return '' + input;

  if (Array.isArray(input)) {
    let acc = "[";
    for (let i = 0; i < input.length; i++) {
      if (input[i] === undefined || typeof input[i] === 'function') input.splice(i, 1);
      acc += `${stringifier(input[i])},`;
    }
    acc = `${acc.substring(0, acc.length - 1)}]`;
    return acc;
  }
  if (typeof input === 'object') {
    let acc = "{";
    for (let key in input) {
      acc += `"${key}":${stringifier(input[key])},`;
    }
    acc = `${acc.substring(0, acc.length - 1)}}`;
    return acc;
  }
}

const test1 = { a: 'hello', b: 123, c: false, d: [1, 2], e: '', f: null, 7: undefined, 8: function () { } };
const test2 = [1, 2, 'hi', 'This is a string', null, undefined, [3, 4], function () { }, ''];
const test3 = 'test';
const test4 = 69;
const test5 = [[6, 9]];
const test6 = true;

console.log('test1;', stringifier(test1), '|', 'JSON:', JSON.stringify(test1));
console.log('test2;', stringifier(test2), '|', 'JSON:', JSON.stringify(test2));
console.log('test3;', stringifier(test3), '|', 'JSON:', JSON.stringify(test3));
console.log('test4;', stringifier(test4), '|', 'JSON:', JSON.stringify(test4));
console.log('test5;', stringifier(test5), '|', 'JSON:', JSON.stringify(test5));
console.log('test6;', stringifier(test6), '|', 'JSON:', JSON.stringify(test6));

I know it's cheap, but it kind of should do the job.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.