0

I searched among many similar questions, but was unable to find a answer to this tricky one. I need a function that removes all the items that are not numbers from the array. But function should modify existing array, not create a new one. I tried to do something like this:

 let array = [1, 'a', 'b', 2];
 array = array.filter(item => typeof item !== "string");
 console.log(array);

 //--> [1, 2]

While I am getting the right result I fell the test on additional points, because according to MDN the result creates a new array with the right result, which I was neglected and not knew completely.

So does anyone have the more experience than me in solving these, from the point of my thoughts I am trying to do something for hours with array.splice and for loops, but nothing near successful.Thank you in advance, and sorry for the long opening post.

7
  • What difference does it make if you modify the existing array? It's very likely to be much faster to create a new array. Commented Oct 28, 2018 at 23:47
  • @Pointy While I agree with you completely, the task was strict and specific and targeting to be a tricky one, harder than it seems. Commented Oct 28, 2018 at 23:51
  • 1
    Would need to iterate and use Array#splice() if requirements are that specific then. Realistically what you have done makes sense as it is if you don't need any of the other data in the future Commented Oct 28, 2018 at 23:51
  • 2
    Note that splicing in a loop is also tricky since it changes the array length. Best to work backwards (from end to beginning) to avoid issues Commented Oct 28, 2018 at 23:56
  • I reckon those are two(array.splice(), for loop) only ways to go, but I tried to iterate first, and not knowing how to check for "string" and removing them from the array at the same time. It could be tricky because each time I do a splice, index numbers of next string is changing. I was trapped there somewhere.. Commented Oct 28, 2018 at 23:59

4 Answers 4

3

You can use Array#splice() in a reverse for loop

Because you are working backwards anything you remove will not affect any of the indexes in the direction you are moving since the original elements still exist from 0 to current position

let array = [1, 'a', 'b', 2];

for (let i = array.length - 1; i >= 0; i--) {
  if (typeof array[i] === "string") {// modify conditional as needed
    array.splice(i, 1);
  }
}

console.log(array);

//--> [1, 2]

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

1 Comment

Thank you charlietfl, it actually worked. I couldn't remember last night typeof array[i] === "string" // Don't code when you are sleepy Thank you very much once again for the missing pieces.
2

You could always try sorting first, instead of looping. For example:

const array = [1, 'a', 'b', '2', 3, 2]

// sort into strings after everything else
array.sort(a => typeof a === 'string')
// splice the string parts away
array.splice(array.findIndex(i => typeof i === 'string'))

console.log(array)

//--> [1, 3, 2]

This works because the sort function is done in place, and basically says if the first compared item is a string, push it back in the sort. This keeps to your constraints, is much less prone to loop bugs, and has the added benefit of only calling splice once.

But really, why are you constrained to mutate the original array? Seems unnecessary, and using filter would make your code so much more readable.

2 Comments

Sorry Matt for the late reply. While I agree with you completely, this was the specific test case, and it was requirement, for the actual exam. I thought they wanted to see my ways of thinking, or something like that. Anyway your code is returning only first number, but thanks for help, you gave me interesting point of view.
@Srki No Worries. I'm curious as to why you are only getting the first number, as the snippet and my small amount of testing seem to work fine. Could you put a quick fiddle together, so I can see where I went wrong?
1

You can do this with a pointer and a while loop that checks whether the pointer has reached the end of the array:

let array = [1, 'a', 'b', 2, 3, 'c', 'd', 3];
let i = 0;

while (i < array.length) {
  if (typeof array[i] === "string") array.splice(i, 1);
  else i++;
}

console.log(array);

Note that if we decide to splice something, we don't increment the pointer.

1 Comment

Thank you slider, you are also providing with a different approach, also good one working great, but I had to give the charlietfl the "checkermark" simply because he arrived first to the finish line. Your solution throws a interesting perspective with a while loop. and also is a proper working one. Thank you once again for the help.
0

I suggest you to use while loop, which will start from the last element and splice the element with the help of Array.splice() method based on the element type, you want to remove.

let array = [1, 'a', 'b', 2];

let i = array.length;

while (i--) {
    if (typeof array[i] === 'string') {
        array.splice(i, 1);
    }
}

console.log(array);

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.