Filter Strings from an Array (Code Challenge)

  • Page Owner: Not Set
  • Last Reviewed: 2019-12-27

Filter out Strings from an Array

Create a function that takes an array of non-negative numbers and strings and return a new array without the strings.

Examples

filterArray([1, 2, "a", "b"]) ➞ [1, 2]

filterArray([1, "a", "b", 0, 15]) ➞ [1, 0, 15]

filterArray([1, 2, "aasf", "1", "123", 123]) ➞ [1, 2, 123]

Notes

  • Zero is a non-negative number.

Tests

Test = (x, y) => x == y ? console.log("Pass", "Yours", x, "Correct:", y) || true : console.log("Fail", "Yours", x, "Correct:", y) || false;

filterArray = x => !+!!1;

Test(filterArray(["m", "c", 1, 2, "a", "b"]), [1, 2])
Test(filterArray(["e", "h", 1, "a", "b", 0, 15]), [1, 0, 15])
Test(filterArray(["r", "r", 1, 2, "aasf", "1", "123", 123]), [1, 2, 123])
Test(filterArray(["r", "i", "jsyt", 4, "yt", 6]), [4, 6])
Test(filterArray(["y", "s", "r", 5, "y", "e", 8, 9]), [5, 8, 9])
Test(filterArray([" ", "t", "i", "o", "u"]), [])
Test(filterArray(["a", "m", 4, "z", "f", 5]), [4, 5])
Test(filterArray(["b", "a", 123]), [123])
Test(filterArray(["!", "s", "$%^", 567, "&&&"]), [567])
Test(filterArray(["w", "r", "u", 43, "s", "a", 76, "d", 88]), [43, 76, 88])

Additional Posts

EZ PZ.

! filterArray = x => x.filter(a => !isNaN(a)).map(a => +a);