Snail problem | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 3

Snail problem

Question: https://www.sololearn.com/post/801304/?ref=app My trial: https://code.sololearn.com/WbSEXApZmOi2/?ref=app Please give me hint where is the mistake 🥺 ( Hint only ) I wanna solve myself 🥺

6th Dec 2020, 11:22 AM
Prasant
Prasant - avatar
13 Answers
+ 3
You are 1st subtracting -2 next adding 7. But it may lead wrong logic. & Snail climbs 7 feet, if it is not the destination, then downs 2 feet. Otherwise it already done. So after adding 7, break loop by condition checking, not after subtracting 2. otherwise if for ex : input 31 but after adding 7, if it reached so subtracting 2 cause 31-2 cause loop another time. Increase k value 1 more. So use if(i>=depth) break;
8th Dec 2020, 7:53 PM
Jayakrishna 🇮🇳
+ 3
Jayakrishna🇮🇳 It's showing 9 instead of 8
9th Dec 2020, 2:20 PM
Prasant
Prasant - avatar
+ 2
Jayakrishna🇮🇳 yes... But it's also not working
9th Dec 2020, 2:19 PM
Prasant
Prasant - avatar
+ 2
You're welcome..
9th Dec 2020, 2:28 PM
Jayakrishna 🇮🇳
+ 2
function main() { var depth = parseInt(readLine(), 10); //tu código va aquí var d = 0; // días var c = 0; // caracol while (c < depth) { c += 7 - 2; d += 1; if (c > depth) { if (c - 2 <= depth) { console.log(d); break; } else if (c > depth) { console.log(d - 1); break; } } } }
6th Jun 2021, 6:38 PM
Kevin Alexander Alvarez Echeverri
Kevin Alexander Alvarez Echeverri - avatar
+ 1
Just add subtract according to the given description.. You are in reverse.. Oh you must subtract after condition check.
6th Dec 2020, 8:49 PM
Jayakrishna 🇮🇳
+ 1
Jayakrishna🇮🇳 Sorry, I couldn't understand I add 7 in 'i', subtract with 2 and increase 'k' with 1 in each loop. Then where is the mistake ?
7th Dec 2020, 2:28 AM
Prasant
Prasant - avatar
+ 1
Jayakrishna🇮🇳 Still it's showing 9 instead of 8 in case of depth = 42
9th Dec 2020, 1:13 AM
Prasant
Prasant - avatar
+ 1
Jayakrishna🇮🇳 Yes, it worked Thank you
9th Dec 2020, 2:27 PM
Prasant
Prasant - avatar
+ 1
console.log(Math.round(depth/5)) Most efficient way
20th Sep 2022, 2:03 PM
VINAYAK ATHREYA B R
VINAYAK ATHREYA B R - avatar
0
var k = 0; function main() { // var depth = parseInt(readLine(), 10); //your code goes here for(let i = 0; i < 42; ){ k++ i+=7; if(i >=42){ break; }else{ i -= 2; } } console.log(k) } main(); //check this code Prasant : The Nothing 😜🙈
9th Dec 2020, 2:22 PM
Jayakrishna 🇮🇳
0
Can someone tell me what I'm doing wrong in these codes: { var depth = parseInt(readLine(), 10); //your code goes here remainder = depth % 5; if (remainder = 0) { days = (depth/5); } else if (remainder = 1) { days = ((depth/5)+1); } else if (remainder = 2) { days = (depth/5); } else if (remainder = 3) { days = ((depth/5)+1); } else if (remainder = 4) { days = ((depth/5)+1); } } //console.log (days);
3rd Oct 2021, 2:32 AM
Betty Chou
Betty Chou - avatar
0
With just one if statment , think out of the box , you dont need for loop at all : if (depth%5<3){ var days=depth/5; }else{ var days=(depth/5)+1; } console.log(parseInt(days));
4th Dec 2021, 6:11 AM
Murtada abed
Murtada abed - avatar