Skip to main content
corrected some grammar error
Source Link
dariosicily
  • 4.1k
  • 1
  • 11
  • 22

Like peterSO said in his answer your code appears complicated and it is not so easy to understand without an explanation of the task, moreover the (a[i] + a[i+1] === 0) condition brings with itself not conform outputs to what you expect. An approach to simplify your code is to use the fact that 0 is a falsy value and combine this fact with a variable storing the number of the ints you are saving in the array :

function zeroTrim(arr){
    let count = 0;
    let foundZero = false;

    for (const elem of arr) {
        if (elem || !foundZero) {
            arr[count++] = elem;
            foundZero = !elem;
        } 
    }

    arr.length = count;
    return arr;
}

console.log(zeroTrim([]));
console.log(zeroTrim([0]));
console.log(zeroTrim([0, 0]));
console.log(zeroTrim([1,2,0,0,0,0,5,7,-6,0,0,0,8,0,0]));

I used let, const instead of var like in your code because for me they help me to read the code.

Like peterSO said in his answer your code appears complicated and it is not so easy to understand without an explanation of the task, moreover the (a[i] + a[i+1] === 0) condition brings with itself not conform outputs to what you expect. An approach to simplify your code is to use the fact that 0 is a falsy value and combine this fact with a variable storing the number of the ints you are saving in the array :

function zeroTrim(arr){
    let count = 0;
    let foundZero = false;

    for (const elem of arr) {
        if (elem || !foundZero) {
            arr[count++] = elem;
            foundZero = !elem;
        } 
    }

    arr.length = count;
    return arr;
}

console.log(zeroTrim([]));
console.log(zeroTrim([0]));
console.log(zeroTrim([0, 0]));
console.log(zeroTrim([1,2,0,0,0,0,5,7,-6,0,0,0,8,0,0]));

I used let, const instead of var like in your code because for me they help me to read the code.

Like peterSO said in his answer your code appears complicated and it is not so easy to understand without an explanation of the task, moreover the (a[i] + a[i+1] === 0) condition brings with itself not conform outputs to what you expect. An approach to simplify your code is to use the fact that 0 is a falsy value and combine this with a variable storing the number of the ints you are saving in the array :

function zeroTrim(arr){
    let count = 0;
    let foundZero = false;

    for (const elem of arr) {
        if (elem || !foundZero) {
            arr[count++] = elem;
            foundZero = !elem;
        } 
    }

    arr.length = count;
    return arr;
}

console.log(zeroTrim([]));
console.log(zeroTrim([0]));
console.log(zeroTrim([0, 0]));
console.log(zeroTrim([1,2,0,0,0,0,5,7,-6,0,0,0,8,0,0]));

I used let, const instead of var like in your code because for me they help to read the code.

Source Link
dariosicily
  • 4.1k
  • 1
  • 11
  • 22

Like peterSO said in his answer your code appears complicated and it is not so easy to understand without an explanation of the task, moreover the (a[i] + a[i+1] === 0) condition brings with itself not conform outputs to what you expect. An approach to simplify your code is to use the fact that 0 is a falsy value and combine this fact with a variable storing the number of the ints you are saving in the array :

function zeroTrim(arr){
    let count = 0;
    let foundZero = false;

    for (const elem of arr) {
        if (elem || !foundZero) {
            arr[count++] = elem;
            foundZero = !elem;
        } 
    }

    arr.length = count;
    return arr;
}

console.log(zeroTrim([]));
console.log(zeroTrim([0]));
console.log(zeroTrim([0, 0]));
console.log(zeroTrim([1,2,0,0,0,0,5,7,-6,0,0,0,8,0,0]));

I used let, const instead of var like in your code because for me they help me to read the code.