1

I have a little code written where I am tracking indexes which I have successfully but finding difficulty removing them from the array so I do same with what I am left with.

 var v = [4, 7, 2,5, 3] 
 var f = []
 for (let i = 1; i < v.length; i += 2){
/*(point1)this line doesn't seem to work*/
 if (v[i] > v[i] - 1)
/*(point 2) Instead of console.log I want to delete every of v[i] */
console.log(v[i])

Output

7
5

Expected Output when v[I] is deleted

 v = [4,2,3]

Preferably I would like to do something like splice v[i] if v[i] > v[i] -1 and get back v as with the spliced elements.

I first tested point 1 with this similar logic in the command line and it worked but.....;

   if ((b[1] -1) > b[0]){console.log(b[2])}

Output

3
```
6
  • v[i] > v[i] - 1 will always be true, since you are comparing if a number is larger than its own value minus one. Do you mean v[i] > v[i - 1]? (i.e. comparing against the previous item in the index) Commented Mar 13, 2020 at 16:07
  • if you can mutate, then use [].filter, it's much simpler and unlike a for-loop, will skip empty (deleted) elements. If you need mutation, use must use [].splice. Commented Mar 13, 2020 at 16:08
  • 1
    Output 6 , 7, there is no 6 in your input. Commented Mar 13, 2020 at 16:10
  • Also what is your expected output? Commented Mar 13, 2020 at 16:10
  • My expected output for v =[4, 7, 2,5, 3] is [4,2,3] and my sincere apologies for the output, I am posting from my phone I made a mistake with the output. Those are results for another test I did on the command line. But the output for this is 7 , 5 as I console log v[i] which should be deleted or removed from the array. The whole thing I am doing here is trying to solve for an algorithm on Hackerrank since yesterday (score 70) to get my second star, and this is the first step of three as I broke down the algorithm to solve. Commented Mar 13, 2020 at 16:30

3 Answers 3

2

Build new array res by eliminating the unwanted elements from array v

var v = [4, 7, 2, 5, 3];
var res = [];
res.push(v[0]);
for (let i = 1; i < v.length; i += 1) {
  if (v[i - 1] > v[i]) {
    res.push(v[i]);
  }
}

console.log(res);

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

Comments

0

To delete a specific index of an array you can use the splice like below.

var fruits = ["apple", "orange", "avocado", "banana"];
//remove one element starting from index 2
var removed = fruits.splice(2, 1);
//fruits is ["apple", "orange", "banana"]
//removed is ["avocado"]

However, regarding to the if logic it will be always true because you are testing if a number is greather than itself subtracted by 1. If you are trying to test if the value in current array position is greather than the value in the previous position, so you should so this.

if (v[i] > v[i-1])

3 Comments

You see the problem here is I won't be manually deleting but the values of the iteration that gets deleted. I want to automate as a function so I use it in another function. Thank you for helping
So do you have to keep the index with a empty value?
No, the index will be v[I]. I am actually comparing the value of v[I] with v[I] - 1. But I can't get the values it seems the logic is only running of the indexes instead of the values of the indexes.
0

Try filter method of array object.

Instead of removing element from current array you can filter out values you want and get new array by using filter method of array object.

var arr = [0, 1, 2, 3];

var filteredArr = arr.filter(function(element, currentIndex, arrObj) {
   //currentIndex and arrObj can be used to form your desired condition but they are 
  //optional parameter
  // below is the filter condition to be applied on each element
  return element > 0;
});

console.log('filteredArr will have all the elements of the array which satisfies the filter condition:', filteredArr);

1 Comment

Thanks. This works but won't serve well the problem I have been given to solve. I will need to repeat the sequence of removing those elements till there are only two elements in my array. So the number of times I had to delete elements is the answer which the third step to this solving this problem. status : Hard , Max score: 70. Success rate : 34%

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.