Skip to content

Instantly share code, notes, and snippets.

@jcolebot
Created January 31, 2022 16:14
Show Gist options
  • Select an option

  • Save jcolebot/5cdb18d6ebfd7a222355e99dd767a71f to your computer and use it in GitHub Desktop.

Select an option

Save jcolebot/5cdb18d6ebfd7a222355e99dd767a71f to your computer and use it in GitHub Desktop.
Filter Out Strings from an Array
// Filter Out Strings from an Array
// Create a function that takes an array of non-negative integers and strings and returns 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 integer.
// The given array only has integers and strings.
// Numbers in the array should not repeat.
// The original order must be maintained.
function filterArray(arr) {
return arr.filter(element => typeof element !== 'string');
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment