-2

How can I check whether an array contains a value, and if so, remove it? PS: For this exercise, I'm not allowed to use anything more than than .pop, .push and .length array functions. My logic is the following: if the specified value is within the array, reorder the array so that the last element of it will contain this value, then remove it with .pop. But how can I find this value and reorder it without using anything more than those array functions I specified above?

This is what I managed to come up with so far:

let array_1 = [1,2,3];

if (array_1 == 2){
  //reorder somehow
  array_1.pop();     
}
console.log(array_1);
lp65
  • 7
  • 2
  • Are you also not allowed to create an additional, temporary array? – shuckster Jul 25 '21 at 00:41
  • 1
    There's a whole bunch of things here. One: using JS conversion is important. Variables and functions use `camelCase`, classes use `CamelCase` (note lower vs. upper initial). Don't put underscores in your variables. Also, don't use `==`, use `===` unless you absolutely know why you're using `==` (they are different operators). Also, if you declare your array as an array of values, then that's what it is, testing whether it's "2" is never going to do anything. – Mike 'Pomax' Kamermans Jul 25 '21 at 00:44
  • 1
    There are [a lot of great Array functions](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array) that you can use here, and I would strongly recommend reading a good tutorial on Arrays first, as part of an intro to JS, to get your familiar with the basics here. – Mike 'Pomax' Kamermans Jul 25 '21 at 00:44
  • As far as I know, there's no issue with temporary arrays. The limitation is not using specific functions like .includes. – lp65 Jul 25 '21 at 00:45

4 Answers4

0

Here's another option using pop

const filter = (array, target) => {
  const newArray = [];

  let tmp;
  while(tmp = array.pop()) {
    if (tmp !== target) {
      newArray.push(tmp)
    }
  }

  console.log(newArray)
  return newArray;
}

filter([1,2,3,4], 2) // [4, 3, 1] Note that it reversed the order of the array!
Gluey1017
  • 36
  • 2
0

Using this approach, you are not creating a new array but modifying it. It uses .pop().

let array_1 = [1, 2, 3];

// Iterate all array
for (let i = 0; i < array_1.length; i++) {
    // While there is a 2 element in the actual index, move all elements (from i index) to the previous index
    while(array_1[i] === 2) {
      for (let j = i; j < array_1.length - 1; j++) {
        array_1[j] = array_1[j + 1];
      }
      // Now remove the last element (since we move all elements to the previous index)
      array_1.pop();
    }
}

console.log(array_1);

Here a snippet so you can try it

let array_1 = [1, 2, 3, 4, 2, 5, 2, 6, 2];

for (let i = 0; i < array_1.length; i++) {
    while(array_1[i] === 2) {
      for (let j = i; j < array_1.length - 1; j++) {
        array_1[j] = array_1[j + 1];
      }
      array_1.pop();
    }
}

console.log(array_1);

This would mantain the order of the array, but without the "2" elements.

AlexSp3
  • 2,201
  • 2
  • 7
  • 24
-1

If you are limited to pop, push and length, you can loop over all elements, check if a given element matches the value you are looking for, and add them to a new array using push.

let array_1 = [1,2,3];
let newArray = [];

for (let i = 0; i < array_1.length; i++) {
  if (array_1[i] !== 2) {
    newArray.push(array_1[i]);
  }
}

console.log(newArray);
nip
  • 1,609
  • 10
  • 20
-1

// using splice
// splice(indexStart, how many, replace with)
// example :
let arr = [0,1,2,3,4,5];

// remove at index 1
arr.splice(1,1);
console.log( arr );

// replace at index 1
arr.splice(1,1,"new 1");
console.log( arr );

// merge index 2 and 3
arr.splice(2,2,"merge 2 and 3");
console.log( arr );

// create 2 new items start at index 2
arr.splice(2,2,"new 2", "new 3");
console.log( arr );
Just ED
  • 27
  • 2