Skip to content

Instantly share code, notes, and snippets.

@LautaroJayat
Created January 27, 2020 18:25
Show Gist options
  • Save LautaroJayat/c58b1d8c5bd4a537fb6b5742f8d9b4d5 to your computer and use it in GitHub Desktop.
Save LautaroJayat/c58b1d8c5bd4a537fb6b5742f8d9b4d5 to your computer and use it in GitHub Desktop.
Array Question
let array = [1, 2, 3, '4', undefined, 'a', [], null];
function onlyNumbers(arr) {
// We create an array to output
let output = [];
// We use forEach() to check if the type of every element is the string 'number'
// because we know that it outputs a lowercase string with the type.
// If true, we push the element to the output.
arr.forEach(e => { if (typeof (e) === 'number') { output.push(e) } })
return output
}
console.log(onlyNumbers(array));
// Wich outputs [ 1 , 2 , 3 ]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment