Why does this code output 0 0? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 1

Why does this code output 0 0?

let depth = 42; let days = 0; let climbed = 0; for (; climbed < depth; days ++) { climbed += 5; if (climbed = depth) { console.log(days); break; } } console.log(days) https://code.sololearn.com/W9VEemHtDpmJ/?ref=app https://code.sololearn.com/W9VEemHtDpmJ/?ref=app

14th Dec 2020, 8:47 AM
Anyaele Nnamdi
6 Answers
+ 1
No, no it is not, the other problem is you are trying to overwork the poor 🐌😢. According to your algorithm, the snail will have to travel an extra day. What did that poor 🐌😢 ever do to you?😉 He can climb 7ft in a day and slides down 2ft when he takes a rest at night. What is 35 + 7? In other words, you need to check if the little fella has reached his goal(42ft) before you subtract 2ft for his resting. -btw The code snippet you wrote does not match the code you linked.
14th Dec 2020, 12:14 PM
ODLNT
ODLNT - avatar
+ 6
Take a closer look at the if statement. Are you trying to asign or compare?
14th Dec 2020, 9:22 AM
ODLNT
ODLNT - avatar
+ 2
Thanks. I just saw it now. Is that the only problem?
14th Dec 2020, 9:53 AM
Anyaele Nnamdi
+ 2
I have solved the problem. That was I code I wrote before getting it correctly. I was just wondering why it was outputting 0 0. It's my first time asking questions on this platform. I didn't know how to do it right. I'm sorry and thank you for your help
14th Dec 2020, 12:15 PM
Anyaele Nnamdi
+ 2
if (climbed = depth) { console.log(days) days = 0 So console.log(days); //output 0
14th Dec 2020, 12:47 PM
Chris Coder
Chris Coder - avatar
+ 1
I'm glad to hear that, and you are welcome.
14th Dec 2020, 12:21 PM
ODLNT
ODLNT - avatar