JavaScript//Module 2//Code project | Sololearn: Learn to code for FREE!
¡Nuevo curso! ¡Todo programador debería aprender IA Generativa!
Prueba una lección gratuita
0

JavaScript//Module 2//Code project

Why is this code correct ? function main() { var depth = parseInt(readLine(), 10); //your code goes here var a = 0; var x = 0; do { a += 7; a -= 2; x++; } while (a <= depth - 3); console.log(x); } And not, This function main() { var depth = parseInt(readLine(), 10); //your code goes here var a = 0; var x = 0; do { a += 7; a -= 2; x++; } while (a <= depth - 5); console.log(x); } Edit 1 The full question: The snail climbs up 7 feet each day and slips back 2 feet each night. How many days will it take the snail to get out of a well with the given depth? Sample Input: 31 Sample Output: 6 Explanation: Let's break down the distance the snail covers each day: Day 1: 7-2=5 Day 2: 5+7-2=10 Day 3: 10+7-2=15 Day 4: 15+7-2=20 Day 5: 20+7-2=25 Day 6: 25+7=32 So, on Day 6 the snail will reach 32 feet and get out of the well at day, without slipping back that night.

3rd Jun 2021, 6:48 PM
Bhagabanta Giri
Bhagabanta Giri - avatar
4 Respuestas
0
You said the 1st one working well.. The problem consists : The snail climbs up 7 feet each day and if it not reached total depth ,then it slips back 2 feet each night. Otherwise it's finish. So add 7feets, if depth is reached stop climbing else subtract 2 feets, a day is over.. Mention that where you not understanding.. pls add link of problem so that if possible I try it and reply....
4th Jun 2021, 11:14 AM
Jayakrishna 🇮🇳
+ 1
Only the difference is in while condition a<=depth-3 and a<=depth-5... read the question again.. else post link or full question here.. All not able to unlock it ..
3rd Jun 2021, 6:54 PM
Jayakrishna 🇮🇳
0
Thanks for the edit. What is your actual logic for depth-3 or depth-5 ? check it if input is 38 May you thinking about only 31 input.. I think the correct logic should be a+2<=depth or a<=depth-2 climbing 7 feets may reach wall fully so then it don't fall 2feets. So no need to subtract , but if not reach then you need to subtract 2. for easily understandable... do { a+=7; x++; if(a<=depth) a-=2; }while(a<=depth); //I think, but not checked it.. don't confuse, if not work..
3rd Jun 2021, 7:32 PM
Jayakrishna 🇮🇳
0
No, still it is not working. I tried all the ways you mentioned. Can you please explain me what the code should essentially consist of?
4th Jun 2021, 5:38 AM
Bhagabanta Giri
Bhagabanta Giri - avatar