0

loop JS for

What is the difference function main() { var depth = parseInt(readLine(), 10); var a=7 var day=1 for(;a<depth;a+=7,day++){ a-=2 } console.log(day) with function main() { var depth = parseInt(readLine(), 10); var a=7 var day=1 for(;a<=depth;a+=7,day++){ a-=2 } console.log(day) First output 6 Second output 9 Why???

14th Dec 2020, 11:58 PM
Ahmad Zulfi Rais
Ahmad Zulfi Rais - avatar
1 Odpowiedź
+ 2
Initial value of a is 7 here. Now if user enters 10 as input to depth variable then with only "<" it will run only 3 times(7,8,9 - 10<10 is false) and with "<=" it will run 4 times(7,8,9,10)
15th Dec 2020, 2:06 AM
Raj Chhatrala
Raj Chhatrala - avatar