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 ?

3

7 Answers 7

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]));

Sign up to request clarification or add additional context in comments.

Comments

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]));

Comments

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]));

Comments

0

Why not just like this:

const data = [1, 2, "aasf", "1", "123", 123];
const filtered = data.filter((d) => typeof d === "number");

Comments

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.

Comments

0

Try this

var arr = [1, 2, "aasf", "1", "123", 123],
    num = arr.filter(item => typeof item === "number")

Comments

0

You can try with this.

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.