Created
January 27, 2020 18:25
-
-
Save LautaroJayat/c58b1d8c5bd4a537fb6b5742f8d9b4d5 to your computer and use it in GitHub Desktop.
Array Question
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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