+ 8

Two different answers

I have tried to solve a challenge in Javascript course by using below code. I got 8 as the answer in jsbin and 9 as the answer in sololearn website. Need help. function main() { var depth = 42; //your code goes here var days=0; var dis = 0; while(dis<=depth) { console.log(dis) days+=1; console.log(days) dis=dis+7-2; } console.log(days) }

13th Apr 2021, 6:24 PM
Pavankumar AN
Pavankumar AN - avatar
31 Réponses
+ 23
1. You should get the depth from input as an integer. 2. You can't have 0 days. Start days at 1. 3. Loop while dis is less than depth. Not less than or equal to. If equal, you're out of the well. 4. In the loop add your climb distance, 7, to dis. 5. Check if dis is now greater than or equal to depth, if yes break out of the loop. 6. After checking 5, if false then subtract your slide, 2, from dis and increase days by 1. 7. After the loop output days.
13th Apr 2021, 6:46 PM
ChaoticDawg
ChaoticDawg - avatar
+ 7
It doesn't give 2 different answers for me. I get 9 on both, which is correct for the code.
13th Apr 2021, 7:03 PM
ChaoticDawg
ChaoticDawg - avatar
+ 2
Phil Andy Graves it's not that. It's more likely that the code that is being run here vs the code that is being run there don't match 100%, but the OP is seeing them as the same. I copied the code from this post and ran it on both platforms and it does exactly the same thing on both.
13th Apr 2021, 7:15 PM
ChaoticDawg
ChaoticDawg - avatar
+ 2
ChaoticDawg your explanation about solving the problem is awesome. Thank you for guiding me towards right path.
14th Apr 2021, 5:45 AM
Pavankumar AN
Pavankumar AN - avatar
+ 2
function main() { var depth = parseInt(readLine(), 10); //your code goes here var day = 1; for(; depth > 7; day++) { depth = depth - 7 + 2; } console.log(day); } Try This.... :)
12th Jun 2021, 8:07 AM
Tanmoy Deyashi
Tanmoy Deyashi - avatar
+ 1
I'm getting the same as ChaoticDawg The output is 9 on both platforms. Pavankumar AN can you try reducing your console logging so that you only log once. Just the one that's after the loop. Here's your code with only one console log. It outputs 9. function main() { var depth = 42; //your code goes here var days=0; var dis = 0; while(dis<=depth) { days+=1; dis=dis+7-2; } console.log(days) } main()
13th Apr 2021, 7:26 PM
CamelBeatsSnake
CamelBeatsSnake - avatar
+ 1
Hi you need to division by 5 and Switch between remaining answers. 0,1,2 and 3,4 with input > 2 function main() { var depth = parseInt(readLine(), 10); //*************** switch(depth%5){ case 0: console.log((depth-(depth%5))/5); break; case 1: console.log((depth-(depth%5))/5); break; case 2: console.log((depth-(depth%5))/5); break; default: console.log(((depth-(depth%5))/5)+1); break; } //***************** }
24th Apr 2021, 9:14 PM
hadi Karimian
hadi Karimian - avatar
0
ChaoticDawg did you understand my question?
13th Apr 2021, 6:55 PM
Pavankumar AN
Pavankumar AN - avatar
0
Yes, write your code as I stated and it will be fixed.
13th Apr 2021, 6:57 PM
ChaoticDawg
ChaoticDawg - avatar
0
ChaoticDawg my question is why the same code is giving two different answer in two different platforms?
13th Apr 2021, 6:58 PM
Pavankumar AN
Pavankumar AN - avatar
0
ChaoticDawg can you run it on jsbin once?
13th Apr 2021, 7:31 PM
Pavankumar AN
Pavankumar AN - avatar
0
Pavankumar AN I have and the output was 9, as I previously mentioned.
13th Apr 2021, 7:35 PM
ChaoticDawg
ChaoticDawg - avatar
0
Pavankumar AN you can figure out why the output should be 9 in your code. The depth is hardcoded to 42. The days and dis both start at 0. The loop executes while the dis is less than or equal to the depth. In the loop, days is increased by 1, then dis is recalculated & saved as the previous dis + 5. Here are the iterations of your loop: // 0 <= 42 is true days = 1 dis = 5 // 5 <= 42 is true days = 2 dis = 10 // 10 <= 42 is true days = 3 dis = 15 // 15 <= 42 is true days = 4 dis = 20 // 20 <= 42 is true days = 5 dis = 25 // 25 <= 42 is true days = 6 dis = 30 // 30 <= 42 is true days = 7 dis = 35 // 35 <= 42 is true days = 8 dis = 40 // 40 <= 42 is true days = 9 dis = 45 // 45 <= 42 is false // the loop ends the days is console logged: 9
13th Apr 2021, 8:35 PM
CamelBeatsSnake
CamelBeatsSnake - avatar
0
ChaoticDawg I assume nothing. Pavankumar AN might be running it on a potato for all we know. Whatever the case, separating that one line and testing between them might lead to the same result.
13th Apr 2021, 8:38 PM
Phil Andy Graves
Phil Andy Graves - avatar
0
Phil Andy Graves "on a potato" I really loled!
13th Apr 2021, 8:40 PM
ChaoticDawg
ChaoticDawg - avatar
0
It doesn't give 2 different answers for me
26th Aug 2021, 10:12 PM
sobhan jaefari
sobhan jaefari - avatar
0
Your problem is right after the loop
19th Nov 2021, 2:19 AM
Piet Seleme
Piet Seleme - avatar
0
Just want to inform everybody the expected output, that sololearn wants to have: //expected output for depth = 31 -> 6 //expected output for depth = 42 -> 8 //expected output for depth = 128 -> 26
6th Feb 2022, 6:36 PM
Mylisa_beth
Mylisa_beth - avatar
0
You can use this: I know this is like "jugaad" But this will always work var depth = 42 dis = 0 while (dis <= depth ) { dis = dis + 5 } day = dis / 5 if (dis-2 >= depth ) console.log(day-1) else console.log(day) This will always work
15th May 2022, 11:28 AM
Prince Gondaliya
Prince Gondaliya - avatar
0
Hi I gues you are talking about the snail on the well question in the bucle test. You are asking just about your code and people could not know what the question is. I had so much troubles with it and after several times it comes out that you need to initialize the var where you sum the deep before compare to the current deep, also remember you need to break the cycle and I saw you are not checking the deep inside the cycle just in the while, or maybe you are talking about another question in thejavascript course but for the momoent is the only question that looks similar to you code to me, so this is the rigth answer let days = 0; let climb = 0; while(climb < depth){ let result = climb + 7; if(result >= depth){ days++; break; } climb = result - 2; days++; //console.log("climb ",climb); } console.log(days);
27th Aug 2022, 1:29 AM
Gustavo Jordan Ramirez
Gustavo Jordan Ramirez - avatar