educative.io

Confusing line in the solution

JS solution.

On the line 13 absolute values of target_diff and smallest_difference are compared. And if if-clause returns true, smallest_difference will be overwritten:

if (Math.abs(target_diff) < Math.abs(smallest_difference)) {
    smallest_difference = target_diff; // save the closest difference
}

On the line 17 there is the same comparison:

if (Math.abs(target_diff) < Math.abs(smallest_difference) ||

Why is it needed since it always returns false? (At least for the problem’s test cases)

Agreed man,

“if abs(target_diff) < abs(smallest_difference)” should be the only code on line 16, the latter part is implied since we move left to right and we’ve never come across a triplet sum smaller than a previous triplet sum where both triplets have the same distance from target sum. I felt this was unnecessary as well.

Could an instructor chime in pls?