Skip to main content
Spelling fix
Source Link
Toby Speight
  • 81.8k
  • 14
  • 101
  • 309

Nested conditionals

There are times it makes sense to nest conditionals, but in tehthe case of your code, it's unnecessary.

This:

    if(i < 2) {
      prod *= arr[i];
      ans.push(-1);
    }
    else {
      if(i === 3) {
        prod *= arr[i];
        ans.push(prod);
      } else if(arr[i] < helper[0]) {
        ans.push(prod);
      } else {
        const min = helper.shift();
        prod /= min;
        prod *= arr[i];
        ans.push(prod);
      }
    }

Is better written:

    if (i < 2) {
      prod *= arr[i];
      ans.push(-1);
    }
    else if (i === 3) {
      prod *= arr[i];
      ans.push(prod);
    } 
    else if (arr[i] < helper[0]) {
      ans.push(prod);
    } 
    else {
      const min = helper.shift();
      prod /= min;
      prod *= arr[i];
      ans.push(prod);
    }

Nested conditionals

There are times it makes sense to nest conditionals, but in teh case of your code, it's unnecessary.

This:

    if(i < 2) {
      prod *= arr[i];
      ans.push(-1);
    }
    else {
      if(i === 3) {
        prod *= arr[i];
        ans.push(prod);
      } else if(arr[i] < helper[0]) {
        ans.push(prod);
      } else {
        const min = helper.shift();
        prod /= min;
        prod *= arr[i];
        ans.push(prod);
      }
    }

Is better written:

    if (i < 2) {
      prod *= arr[i];
      ans.push(-1);
    }
    else if (i === 3) {
      prod *= arr[i];
      ans.push(prod);
    } 
    else if (arr[i] < helper[0]) {
      ans.push(prod);
    } 
    else {
      const min = helper.shift();
      prod /= min;
      prod *= arr[i];
      ans.push(prod);
    }

Nested conditionals

There are times it makes sense to nest conditionals, but in the case of your code, it's unnecessary.

This:

    if(i < 2) {
      prod *= arr[i];
      ans.push(-1);
    }
    else {
      if(i === 3) {
        prod *= arr[i];
        ans.push(prod);
      } else if(arr[i] < helper[0]) {
        ans.push(prod);
      } else {
        const min = helper.shift();
        prod /= min;
        prod *= arr[i];
        ans.push(prod);
      }
    }

Is better written:

    if (i < 2) {
      prod *= arr[i];
      ans.push(-1);
    }
    else if (i === 3) {
      prod *= arr[i];
      ans.push(prod);
    } 
    else if (arr[i] < helper[0]) {
      ans.push(prod);
    } 
    else {
      const min = helper.shift();
      prod /= min;
      prod *= arr[i];
      ans.push(prod);
    }
Source Link
Chris
  • 1.8k
  • 1
  • 4
  • 21

Nested conditionals

There are times it makes sense to nest conditionals, but in teh case of your code, it's unnecessary.

This:

    if(i < 2) {
      prod *= arr[i];
      ans.push(-1);
    }
    else {
      if(i === 3) {
        prod *= arr[i];
        ans.push(prod);
      } else if(arr[i] < helper[0]) {
        ans.push(prod);
      } else {
        const min = helper.shift();
        prod /= min;
        prod *= arr[i];
        ans.push(prod);
      }
    }

Is better written:

    if (i < 2) {
      prod *= arr[i];
      ans.push(-1);
    }
    else if (i === 3) {
      prod *= arr[i];
      ans.push(prod);
    } 
    else if (arr[i] < helper[0]) {
      ans.push(prod);
    } 
    else {
      const min = helper.shift();
      prod /= min;
      prod *= arr[i];
      ans.push(prod);
    }