Quiz question dobut in javascript

var num2=10;
var num3=20;
var num4=30;
var sum1= ++num2 + num3 + num4++;
var sum2= num2++ + ++num3 + ++num4;
var sum3= num2++ + num3++ + num4++;
console.log(sum1+sum2+sum3)
when we can run it vscode i can got 190.
in quiz answer i can see 192.
please explain it with solution line by line

@thakurvipinsingh2711
Hi! you have made one mistake Let me share the correct code
var num2=10;
var num3=20;
var num4=30;
var sum1= ++num2 + num3++ + num4++;
var sum2= num2++ + ++num3 + ++num4;
var sum3= num2++ + num3++ + num4++;
console.log(sum1+sum2+sum3)

From the above
var sum1= ++num2 + num3++ + num4++;
check this line again you have forgot to add ++ this symbol with num3.
Now run the above code you will get the answer 192.

explain the all three line of sum .with there value

@thakurvipinsingh2711
Sure !
In sum1, num2 is incremented by 1 using the pre-increment operator (++num2), then added to num3 which is incremented by 1 using the post-increment operator (num3++), and added to num4 which is also incremented by 1 using the post-increment operator (num4++). The resulting value of sum1 is 11 + 20 + 30, which is 61.
In sum2, num2 is incremented by 1 using the post-increment operator (num2++), then added to num3 which is incremented by 1 using the pre-increment operator (++num3), and added to num4 which is also incremented by 1 using the pre-increment operator (++num4). The resulting value of sum2 is 12 + 21 + 31, which is 64.
In sum3, num2 is incremented by 1 using the post-increment operator (num2++), then added to num3 which is incremented by 1 using the post-increment operator (num3++), and added to num4 which is also incremented by 1 using the post-increment operator (num4++). The resulting value of sum3 is 13 + 22 + 32, which is 67.

Therefore, the final value of sum1 + sum2 + sum3 is 61 + 64 + 67, which is 192. So the code will output 192.

1 Like