+ 6

# 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
+ 20
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
+ 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
+ 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
+ 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
+ 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
+ 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
+ 1
Pavankumar AN The only other thing I can think of that may possibly effect the outcome of the code on jsbin is make sure that you're putting the code in the Javascript tab and that you have plain Javascript selected (blue writing 'Javascript' with down arrow in upper left corner of page mobile). Edit, nope I just tried all the options, and they give either the same result, an error, or don't run. https://jsbin.com/zejiqomisi/edit?js
13th Apr 2021, 7:41 PM
ChaoticDawg
+ 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
0
ChaoticDawg did you understand my question?
13th Apr 2021, 6:55 PM
Pavankumar AN
0
Yes, write your code as I stated and it will be fixed.
13th Apr 2021, 6:57 PM
ChaoticDawg
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
0
ChaoticDawg can you run it on jsbin once?
13th Apr 2021, 7:31 PM
Pavankumar AN
0
Pavankumar AN I have and the output was 9, as I previously mentioned.
13th Apr 2021, 7:35 PM
ChaoticDawg
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
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
0
Phil Andy Graves "on a potato" I really loled!
13th Apr 2021, 8:40 PM
ChaoticDawg
0
It doesn't give 2 different answers for me
26th Aug 2021, 10:12 PM
sobhan jaefari
0
i also tried , but till the answer came same.
9th Sep 2021, 11:09 AM