Skip to content

Instantly share code, notes, and snippets.

@cheriimoya
Created March 20, 2018 18:42
Show Gist options
  • Save cheriimoya/fc23ce59677ece303332fe3437fe43c9 to your computer and use it in GitHub Desktop.
Save cheriimoya/fc23ce59677ece303332fe3437fe43c9 to your computer and use it in GitHub Desktop.
function keepNumbers(mixedarray){
//create empty array to store the numbers in
let numberarray = [];
//for each element in the array call the anonymous function...
mixedarray.forEach(
//... right here
function(element) {
//check if the element type is "number"
if(typeof(element) == "number"){
//if it is, add it to the number array
numberarray[numberarray.length] = element;
}//otherwise do nothing
}
);
//return the array that just contains numbers
return numberarray;
}
//function is beeing called with a mixed element array
keepNumbers(["one", 4, 22, "jdsljf", [2]]);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment