Return Only Strings

Ayumi Tanaka
Algorithm Novice
Published in
Sep 30, 2020

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

const filterArray = function(arr) { console.log(arr.filter(arr => typeof arr === 'number'));}filterArray([1, "2", "a", 3]); // should be [1, 3]
  1. Extract elements and create a new array with filter() method.
  2. Add condition with typeof operator, the element should be extracted when it is number.

--

--