Confusing Increment (JS) | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

Confusing Increment (JS)

Every time I increase the number of increment the digits in the ouput decreases.one x++ gives 5 output, 2 x++ gives 3, 3 gives 1. https://code.sololearn.com/WJKCn8FhSJCY/?ref=app

22nd Aug 2022, 5:39 AM
Riyad Rahman [π©]
Riyad Rahman [π©] - avatar
3 Answers
+ 2
hey man see and understand the flow of code. first it is printing orginal value of x (1) and then it does increment by two postfix that means (3) then it checks condition wheather it is greater than 5 it means noo then it again goes up and increment two timess and now it retains 5 that means x = 5 now it again goes up and increment and now it becomes 7 it means it is greater than 5 so it breaks and return it will not print 7 and while loop gets false..
22nd Aug 2022, 5:56 AM
Davinder Kumar
Davinder Kumar - avatar
+ 1
Hi! try to investigate this problem yourself Visualize your code execution (Python, Java, C, C++, JavaScript, Ruby) https://pythontutor.com
22nd Aug 2022, 5:52 AM
Yaroslav Vernigora
Yaroslav Vernigora - avatar
0
I've just got it.It is all because of the condition which is not letting to give anything more than 5.
22nd Aug 2022, 7:53 AM
Riyad Rahman [π©]
Riyad Rahman [π©] - avatar