I just answered the same question in another post.
Yes, the solution in the pdf is right.
What happens here is a little weird, but it works...will try explaining.
Consider two persons. Following are the marks they got in Physics, Chemistry, Maths, and the total.
Person1: 40 (Physics), 40 (Physics), 70 (Maths), 150 (Total)
Person2: 30 (Physics), 10 (Chemistry), 50 (Maths), 90 (Total)
As the code stands now, Proc2 will return -10 (30 - 40) for Physics, -30 (10 - 40) for Physics, -20 (50 - 70) for Physics and -60 (90 - 150) for the Total.
It will add the first 3 to get -10 + - 30 + -20 = -60. THis matches with the difference in the Total, and hence Z is incremented.
It works as expected.
Now, if we change line 20 as follows:
if (C > D) {
return C - D
}
it would return +10, +30, +40, +60. In this case, the sum of the first 3 should match with the Total, and hence Z incremented.
Why would you want to change a line unnecessarily, if both worked well? THat's the reason, line 20 is not an error.
On Saturday, November 21, 2020 at 3:03:22 PM UTC+5:30 Palak Sinha wrote:
Can someone please explain this??