Is there a simpler way | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

Is there a simpler way

function main() { var d = parseInt(readLine(), 10); sum = 0 i=1 a=5 while(sum<d){ if(sum >= d-7){ a = 7; console.log(i) } sum+=a i++ } }

28th Sep 2021, 12:43 PM
Joel Sebastian Jijo
Joel Sebastian Jijo - avatar
5 Answers
0
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. This is the question
28th Sep 2021, 1:00 PM
Joel Sebastian Jijo
Joel Sebastian Jijo - avatar
+ 1
Ok thanks i will look for them.
28th Sep 2021, 3:36 PM
Joel Sebastian Jijo
Joel Sebastian Jijo - avatar
+ 1
Probably this work function main() { var depth = parseInt(readLine(), 10); //your code goes here var day = 0; var snailPosition = 0; var morning = 7; var night = 2; while(true){ snailPosition += morning day++; if(snailPosition >= depth){ break; } else{ snailPosition -= night; } } console.log(day); }
28th Sep 2021, 5:51 PM
Hamza Akburak
Hamza Akburak - avatar
0
Hi Joel! Considering it's a code coach task, meaning it's common and many had given their attempts for it ... I would suggest you to try and search for similar threads in the forum, and also similar code examples in Code Playground for inspiration on how to "simplify" the code 👍
28th Sep 2021, 1:40 PM
Ipang
0
Thanks it does work
29th Sep 2021, 3:18 AM
Joel Sebastian Jijo
Joel Sebastian Jijo - avatar