-2

I want to remove the negative elements of the copied array by pop() method but it removes only the last element of the array. Anyone knows where's the problem? The code is in the imageenter image description here

I tried:

const scores = [128, 0, -8, 50, 2, 3];
const betterScores = Array.from(scores);

for (const s of betterScores) {
  if (s < 0) {
    betterScores.pop(s);
  }
}

console.log(betterScores);

I wanted to have betterScores = [128,50,2,3] but I got [128,0,-8,50,2].

3

6 Answers 6

2

Use filter instead of for loop:

const betterScores = scores.filter(s => s > 0);
Sign up to request clarification or add additional context in comments.

2 Comments

This answer is technically correct but it does not help the OP improve their knowledge. Add a link to the documentation of Array.filter() to improve it. You can also add a link to the documentation of Array.pop() to help the OP understand why their approach does not work.
@axiac, I agree. But this aspect also depends on the personal eagerness to learn more.
1

pop method does not expect any arguments, it automatically removes last element of array. To remove element at an index you can use splice method.

const arrayOfNumbers = [1, 2, 3, 4];

arrayOfNumbers.splice(1, 1);

console.log(arrayOfNumbers); // [1, 3, 4]

So to remove one element at an index you would use splice(n, 1), where n is index if element you are removing and 1 is count of elements(for your case you would default 2nd argument to 1 and just give right index. Also to have access to index for of loop wont work. Use basic for loop or forEach to have access to index

1 Comment

This does not answer the question.
1

You can do easily by filter in javascript.

Try this:

Filter method doc: Filter Method

const scores = [128, 0, -8, 50, 2, 3];
const betterScores = Array.from(scores);

const result = betterScores.filter(item => item > 0);

console.log(result);

3 Comments

This answer is technically correct but it does not help the OP improve their knowledge. Add a link to the documentation of Array.filter() to improve it. You can also add a link to the documentation of Array.pop() to help the OP understand why their approach does not work.
So, Which method do you want?
@PriyenMehta, The splice solution looks good.
1

filter is the best approach to use to solve this problem as the other users have commented.

To answer the specific question about why your code doesn't work:

The pop() method removes the last element from an array and returns that element. This method changes the length of the array. (my emphasis)

If you really wanted to use a loop to remove elements you might use a simple for loop and splice elements from a particular index. But since the length of the array gets shorter when you remove elements it's usually best to start from the end of the array, and iterate toward the start.

const scores = [128, 0, -8, 50, 2, 3];

for (let i = scores.length - 1; i >= 0; --i) {
  if (scores[i] <= 0) {
    scores.splice(i, 1);
  }
}

console.log(scores);

Comments

0

I'm sure you are not aware of pop method of array, It pop last element of you array. If you task is to only get positive number then using filter would be better solution

const scores = [128, 0, -8, 50, 2, 3];
const result = scores.filter(item => item >= 0);

console.log(result);

but if you task is to do using some method to manually remove element form array you can use splice method something like this

const scores = [128,0,-8,50,2,3];
const betterScores = Array.from(scores);

for(const s in betterScores){
    if(betterScores[s] < 0){
  betterScores.splice(s,1);
    }
}
console.log(betterScores);

2 Comments

You shouldn't be using for...in to iterate over arrays.
Thanks! We aren't allowed to use filter() at this time. The second solution is perfect.
0

you may try this code:

const scores = [128, 0, -8, 50, 2, 3];
const betterScores = Array.from(scores);

for (const s of betterScores) {
  if (s < 0) {
    betterScores.splice(betterScores.indexOf(s), 1)
  }
}

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.