Error in omitting semicolon - can someone explain why?

118 Views Asked by At

Can someone please explain why if I omit the semicolon in the line while(nums[right-1] == nums[right--]);that I get a syntax error? I generally understand the point of semicolons, but why this specific line and not the others?

var threeSum = function(nums) {
    nums.sort((a,b)=>a-b);
    let result = [];
    for (let i=0;i<nums.length;i++) {
        let t = -nums[i];
        let left = i+1, right = nums.length-1;
        while(left < right) {
            if (nums[left] + nums[right] == t) {
                result.push([nums[i], nums[left], nums[right]]);
                while(nums[left+1] == nums[left++]);
                while(nums[right-1] == nums[right--]);
            } else if (nums[left]+nums[right] < t) left++;
            else right--;
        }
        while(nums[i+1]==nums[i]) i++
    }

    return result;

};
2

There are 2 best solutions below

1
On BEST ANSWER

A loop needs something to loop over. Basically either a statement or a code block. For example:

while (something) {
  this.isInACodeBlock();
}

or:

while (something)
  this.isAStatement();

Keep in mind that a statement can be a no-op. A semicolon by itself serves that purpose. And since carriage returns don't matter to the language, this is a loop with a statement:

while (something);

Taking a step out, this loop is within a code block, which ends right after the loop:

if (somethingElse) {
  while (something)
}

If there was just a statement following the while loop then it would be syntactically correct, even if the behavior is unexpected or a bug. But there's a closing curly brace.

So the interpreter is expecting either a statement or an opening block, but it's encountering a closing block. Which is an error.

5
On

Good explanation from @David, I will add that to avoid theses kind of error you should consider to follow a styling guide like airbnb. Make your code easier to read.

Way to achieve that: Replace ++ by +=. Comment the main parts of your code ... etc

A good code is a code you can understand by reading it. I should have been understand what threeSums function is doing and why you are performing theses while just by looking at it.

// the function do blabla
function threeSums(nums) {
  nums.sort((a, b) => a - b);

  const result = [];

  for (let i = 0; i < nums.length; i += 1) {
    // what is t?
    const t = -nums[i];

    let left = i + 1;
    let right = nums.length - 1;

    while (left < right) {
      // what is the case covered by this if?
      if (nums[left] + nums[right] == t) {
        result.push([
           nums[i],
           nums[left],
           nums[right],
        ]);

        while (nums[left + 1] == nums[left]) {
          left += 1;
        }

        while (nums[right - 1] == nums[right]) {
          right -= 1
        }
      } else if (nums[left] + nums[right] < t) {
        left += 1;
      } else {
        right -= 1;
      }
    }

    while (nums[i + 1] == nums[i]) {
      i += 1;
    }
  }

  return result;
};