0

Trying to make a function that removes only the "strings" from the array. I'm looking to only have the numbers left. I've already accomplished this by adding numbers only to a newArray, and I was looking into the splice method but could not figure out how to write it. As you see in code delete works, but returns undefined in its spot.

function numbersOnly(arr){

    for(var i = 0; i < arr.length; i++){
        if(typeof arr[i] === "string"){
        delete arr[i];
        }
    }
    console.log(arr);
 }
numbersOnly([1, "apple", -3, "orange", 0.5, 22, "hello", 6])

returns [1, undefined, -3, undefined, 0.5, 22, undefined, 6]

2

6 Answers 6

1

var arr = [1, "apple", -3, "orange", 0.5, 22, "hello", 6];
var out = arr.filter(function(f) { return typeof(f) !== 'string';});
console.log( out );

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

Comments

0

If you delete sth in an array, there stays an empty space which is undefined. You need to shift the array, so replace

 delete arr[i];

with

arr=arr.slice(0,i).concat(arr.slice(i+1));
i--;

or using splice:

arr.splice(i,1);
i--;//dont jump over this index

or using a for loop:

for(var b=i;b<arr.length-1;b++){
  arr[b]=arr[b+1];
}
arr.length-=1;
i--;

Alternatively you could filter:

arr.filter(el=>typeof el==="number");//numbers only
//or
arr.filter(el=>typeof el!=="string");//not stringd

3 Comments

If you use splice or similar, all the indexes shift, so i has the wrong value afterwards.
@Jonas w-That's what I was looking for! Thanks
@robert amato youre welcome ;)
0

You would want to use array.splice() instead of delete, which is for properties.

Comments

0

I would have a go another way around

function numbersOnly(arr){
let numbers = [];
for(var i = 0; i < arr.length; i++){
    if(typeof arr[i] === "number"){
    numbers.push(arr[i]);
    }
}
console.log(numbers);

}

2 Comments

Why are you mixing let and var?
I just copied his code and added my stuff , I am not using var at all
0

use .splice(index, count)

function numbersOnly(arr) {
  for(var i = 0; i < arr.length; i++) {
    if(typeof arr[i] === "string") {
      arr.splice(i, 1);
    }
  }
  console.log(arr);
}
numbersOnly([1, "apple", -3, "orange", 0.5, 22, "hello", 6]);

3 Comments

Breaks if there are multiple strings in a row in the input array.
Breaks? I dont understand what you're talking
Try running your code on [ "foo", "bar", "baz" ]. What do you expect the result to be? What do you actually get?
0

If the array's cell contains a number and that number yields true for if Number(). Then that value will be added to a secondary array and returned.

  function numbersOnly(arr){ 

      var numbersarr = [];
      var i;
      var k = 0;
      for (i=0; i< arr.length ;i++) {
    
      if (  Number(arr[i])   ) {
           
            numbersarr[k++] = arr[i];
         }
     }
      
      console.log(numbersarr);
      return numbersarr;
     }

    var strarr = [1, "apple", -3, "orange", 0.5,
                  22, "hello", 6 , "car", "123" , 
                  "zoo", "456"];
        
    numbersOnly(strarr);    
    numbersOnly(["foo","bar", "something"]);
    

And this is how your output will be:

  [1, -3, 0.5, 22, 6, "123", "456"]

9 Comments

Did you even try this? It should remove all elements (they're all strings) but it doesn't. Also try with an input of [ "foo", "bar", "baz" ].
Yes, in fact, I tried it. I ran the code first, then I brought it here. I updated my answer to show the output.
Your code left "123" and "456" in the array even though they're strings. This is wrong. (Also, you should really try running it on [ "foo", "bar", "baz" ].)
@melpomene He did not specify in his requirement that "123" is not considered a number. and 123 a number.
Javascript itself specifies that "123" is a string (and 123 is a number). But even if you ignore that, numbersOnly([ "foo", "bar", "baz" ]) outputs [ "bar" ]. Surely you don't think that "bar" is considered a number?
|

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.