0

I'm trying to make a function that takes an array as input and a target, and then iterates the array to check if the current index is equal to the target. If so, it will splice the current index from the array.

Everything works fine so far, however when i implement an if statement to check if the index is at the end of the array and then check if the result array is equal to the input array. I really don't know why this is taking me so long it's kind of embarrassing... Here's my code:

let array = ['fox', 'tiger', 'elephant', 'jaguar', 'wolf', 'deer', 'hog', 'dhole', 'leopard', 'eagle', 'bear'];

const splice = (arr, target) => {
    //creates empty result array
    let result = [];
    //iterate through the input array and push each item to the result array
    for(let i = 0; i < arr.length; i++) {
        result.push(arr[i]);
    }
    let j = 0;
    //iterate through result array
    while(j < result.length) {
        if (result[j] === target) {
            result.splice(j, 1);
        }
        
        //i have tried this multiple times with the if statement in and out of the loop
        if (j === result.length && result === arr) {
            //throw new Error(`${target} was not found in the array`);
            console.log({result, arr, j});
            return 'equal';
        } else if (j === result.length && result !== arr ) {
            console.log({result, arr, j});
            return 'different';
        }
        j++;
    }
    
};

//should return 'equal' but returns 'different'
console.log(splice(array, 'turtle'));

//should return 'different' but returns undefined
console.log(splice(array, 'bear'));

3 Answers3

1

Hope this help

const splice = (arr, target) => {
    //creates empty result array
    let result = [];
    //iterate through the input array and push each item to the result array
    for(let i = 0; i < arr.length; i++) {
        result.push(arr[i]);
    }
    let j = 0;
    //iterate through result array
    while(j < result.length) {
        if (result[j] === target) {
            result.splice(j, 1); 
            /* since you modified the length of result array here,
            u can simply check the length of result array
            with the original array to see if there's a different */
        }
        // result === arr will always resolve to fasle as they are not the same object, so check the length instead
        if (j === result.length && result.length !== arr.length) {
            //throw new Error(`${target} was not found in the array`);
            console.log({result, arr, j});
            return 'equal';
        } else if (j === result.length && result.length === arr.length ) {
            console.log({result, arr, j});
            return 'different';
        }
        j++;
    }
    // a better way of doing it is to move the if check outside of while loop to avoid it run multiple time
    if (result.length !== arr.length) { // different length, that mean we found the target and the result array got modified 
        console.log({result, arr, j});
        return 'equal';
    } else {
        console.log({result, arr, j});
        return 'different';
    }

};
N_A_P
  • 41
  • 3
1

Here you can use this logic to compare two arrays are same or not :

let arr1 = [1, 2, 3, -2, null];
let arr2 = [1, 2, 3, -2, null];

let bool = true;

let i;
for (i = 0; i < arr1.length; i++) {
  if (arr1[i] !== arr2[i]) {
    bool = false;
    break;
  }
}
if (arr1[i] === arr2[i]) bool = true;
if (arr1[i] !== arr2[i] || arr1.length !== arr2.length) bool = false;

if (bool) console.log("same");
else console.log("different");
Jerry
  • 1,005
  • 2
  • 13
1

As others told you in comments, since arrays are object in JavaScript, you cannot compare them with a simple ===.

To compare 2 arrays, first, you have to decide what do you means by arr1 === arr2.

For example: does [A,C,B] concider equals to [A,B,C]? how about [A,B,C, ]?

Lets say you need the two arrays to have...

1.) same length,

2.) index value must matches.

you can do something like this:

const arr1 = [1,2,3,"A"];
const arr2 = [1,2,3,"A"];

function compareArr(A,B) {
  return (A.length === B.length) && A.every((v,i) => B[i] === v)
}

console.log(compareArr(arr1,arr2))
Ping
  • 891
  • 1
  • 2
  • 10