2

I'm new in Javascript, I want to Create a function that takes an array of non-negative integers and strings and return a new array without the strings.

Examples filterArray([1, 2, "aasf", "1", "123", 123]) ➞ [1, 2, 123]

I've tried this code :

function filterArray(arr) {
    var j=0;
    var numArr;
    for (let i=0;i<length.arr;i++)
    {
        if (typeof arr[i] ==="number") 
        {
            numArr[j]=arr[i];
            j++;
        }       
    }
    return numArr
}
console.log(filterArray([1, 2, "aasf", "1", "123", 123]));

but i didn't arrive to run my code, can someone helps me ?

7 Answers7

2

using the filter method https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/filter

function filterArray(arr) {
    return arr.filter(x => typeof x === "number");
}

console.log(filterArray([1, 2, "aasf", "1", "123", 123]));
Martin Meli
  • 452
  • 5
  • 18
0

1) First the loop should be run up to arr.length not length.arr.

2) No need to use another counter variable j, you can use simply array as

var numArr = [];

and you can use push method to add elements in the array at the end.

function filterArray(arr) {
  var numArr = [];
  for (let i = 0; i < arr.length; i++) {
    if (typeof arr[i] === "number") {
      numArr.push(arr[i]);
    }
  }
  return numArr;
}

console.log(filterArray([1, 2, "aasf", "1", "123", 123]))
// ➞ [1, 2, 123]

Other alternatives

function filterArray(arr) {
  // return arr.filter((el) => +el === el);
  // or
  // return arr.filter((el) => typeof el !== `string`);
  // or
  return arr.filter(el => typeof el === "number");
}

console.log(filterArray([1, 2, "aasf", "1", "123", 123]));
DecPK
  • 24,537
  • 6
  • 26
  • 42
0

You can use Array.filter() to filter your elements based on a condition given.

function filterArray(arr) {
    return arr.filter(el => typeof el === 'number');
}
console.log(filterArray([1, 2, "aasf", "1", "123", 123]));
Nicolae Maties
  • 2,476
  • 1
  • 16
  • 26
0

Why not just like this:

const data = [1, 2, "aasf", "1", "123", 123];
const filtered = data.filter((d) => typeof d === "number");
Stiegi
  • 1,074
  • 11
  • 22
0

Instead of giving the most optimized answer, I will try to address a few problems with your code. You can look at any other answer to use the best code:

  1. length.arr should be arr.length. That is how JS works.
  2. var numArr, if you will access numArr[0] this will give you an error. Initialize with an empty array.
function filterArray(arr) {
    var j=0;
    var numArr = [];
    for (let i=0;i<arr.length;i++) 
        { 
        if (typeof arr[i] ==="number") 
        {numArr[j]=arr[i];
        j++;
        }       
        }
    return numArr
}


Make these changes and your code works.

Tushar Shahi
  • 16,452
  • 1
  • 18
  • 39
0

Try this

var arr = [1, 2, "aasf", "1", "123", 123],
    num = arr.filter(item => typeof item === "number")
JS_INF
  • 467
  • 3
  • 10
0

You can try with this.

const arr = [1, 2, "aasf", "1", "123", 123]
const filteredData = arr.filter((item) =>  Number(item) === item );