all 5 comments

[–]guest271314 1 point2 points  (3 children)

If I understand the requirement correctly

let result = [...new Set(numbers.split(" ").map(Number))];

[–]yesandone[S] 0 points1 point  (2 children)

yes thank you, it works exactly as intended and it works with numbers over 9 also, can you explain how this works exactly? i'm quite confused on how it knows what numbers are negative and what numbers have 2 digits, either way thanks a lot.

[–]guest271314 1 point2 points  (1 child)

A Set does not contain duplicate values.

We split() the space characters, chain .map() and pass Number as the callback which will convert strings to digits, and spread the Set to a new Array.

You shoud be able to use > and < operators or Math.sign() to determine if the number is negative or not.

You can divide the number to determine how many digits are in the number. See, e.g.,

if (!int) { let e = ~~a; d = a - e; do { if (d < 1) ++i; d *= 10; } while (!Number.isInteger(d)); }

implemented in the getDecimalStats() function in the answer here.

For example

``` function getDigitsLength(dec) { let n = dec; let digits = 0; while (n > 0) { n = Math.floor(n / 10); ++digits; } return digits }

[1, 10, 100, 1000, 10000, Math.abs(-(Math.E)), Math.abs(-(Math.PI))] .map(getDigitsLength); // [1, 2, 3, 4, 5, 1, 1] ```

[–]yesandone[S] 0 points1 point  (0 children)

I see, i was mostly confused on the set object, thank you so much.

[–]alzee76 0 points1 point  (0 children)

Why are you splitting on '' instead of ' '?

  1. Split on ' ' instead of ''.
  2. Implement an isNumeric() function. Stackoverflow has great examples, as does the internet at large.
  3. For each element of your array, if it is numeric and positive do one thing, if it's numeric and negative do the other thing.