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
  • 4,001
  • 17
  • 59
  • 109

5 Answers5

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
  • 35,834
  • 5
  • 42
  • 73
  • @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
  • 6,267
  • 6
  • 33
  • 47
1
var numberArray: any[];
numberArray.filter(Number)

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

user11100340
  • 53
  • 1
  • 8
1

I worked out a simple answer that will work as well using the same logic required to solve your problem. I used it on an example where you have an array of temperature values, and you want to remove all the values which are strings from the existing array, then populate the new empty array.You can use typeof operator to identify the type of value in the temperatures array at position i which is the index of that array element. If the type of that value is not a string then push the value of the temperatures array at the current index position to the new array.

const temperatures = [3, -2, -6, -1, 'error', 9, 13, 17, 15, 14, 9, 5];

const cleanTemperatures = [];

for (let i = 0; i < temperatures.length; i++) {
  if (typeof temperatures[i] !== 'string') {
    cleanTemperatures.push(temperatures[i]);
  }
}
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