2

Other articles talk about removing strings from an array based on a search term.

But I'm trying to indentify which elements are strings and which elements are numbers in an array, and then remove all strings to return a new array.

function filter_list(l) {

  let newArray = []; 

  for (let i = 0; i < l.length; i ++) {

     if (i !== "^[a-zA-Z0-9_.-]*$") {
       newArray = newArray + i;
     }
  }

  return newArray;

}

This is returning 0123.

  1. Why is it not returning an array?
  2. Why is if (i !== "^[a-zA-Z0-9_.-]*$") not working? How else can I check for when an element is a string (something in quotes) within the array?

https://www.codewars.com/kata/list-filtering/train/javascript

Thanks

HappyHands31
  • 3,791
  • 12
  • 51
  • 90

4 Answers4

4

You can is typeof keyword. and filter(). I have tested the code its passing all tests in codewars.

Using ES6 Arrow Function

function filter_list(l) {
  return l.filter(x => typeof x === "number");
}
console.log(filter_list([1,2,'a','b']))

Without Arrow Function

function filter_list(l) {
  return l.filter(function(x){
      return typeof x === "number"
    });
}
console.log(filter_list([1,2,'a','b']))

Using Simple Loops

function filter_list(l) {
  let newArr = [];
  for(let i = 0;i<l.length;i++){
    if(typeof l[i] === "number") newArr.push(l[i]);
  }
  return newArr
}
console.log(filter_list([1,2,'a','b']))
Maheer Ali
  • 34,163
  • 5
  • 36
  • 62
  • @HappyHands31 In this case it's just a shortcut for `function(x) { return typeof x !== "string"; }` – Andreas Mar 28 '19 at 17:09
  • 2
    Why `!== "string"` and not `=== "number"`? It will work in with the given inputs, but would fail for `[{"foo": "bar}, 0, 1]` – Andreas Mar 28 '19 at 17:11
  • 2
    _"`=>` just mean implicit `return`"_ - That's non-sense... _"An arrow function expression is a syntactically compact alternative to a regular function expression, although without its own bindings to the this, arguments, super, or new.target keywords." (Source: [MDN](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Functions/Arrow_functions))_ – Andreas Mar 28 '19 at 17:16
1

Regex is not good way to parse such table. Try isNaN

console.log(
    [1,2,3,4,5, 'a', 'b', 1, 3].filter(item => !isNaN(item) ? item : '')
)

If you want less hacky way try

function filter_list(l) { 
// l is very bad name, because look similar to i

  let newArray = []; 

  for (let i = 0; i < l.length; i ++) {
      !isNaN(l[i]) ? newArray.push(l[i]) : ''
  }
  return newArray;
}

or even

  for (let i = 0; i < l.length; i ++) {
      !isNaN(l[i]) ? newArray[i] = l[i] : ''
  }

Hovewer, this task can be done with regexes, but I cannot recommend this solution.


[1,2,3,4,5, 'a', 'b', 1, 3].join(' ').replace(/\D/gm, '').split('')

Kamil Naja
  • 5,402
  • 5
  • 30
  • 44
1
var numberArray: any[];
numberArray.filter(Number)

Using this you can filter only numbers in an array and then can performe what you want.

0

function filter_list(l) {
  return l.filter(x => typeof x === "number");
}
console.log(filter_list([1,2,'a','b']))
  • Welcome to Stackoverflow. Please include a description of what the code is doing and how it addresses the problem. The scrip-only answers do not add much insight for the reader. – pegah Oct 26 '21 at 12:30