+5

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) }

4/13/2021 6:24:15 PM

Pavankumar AN

+19

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.

+7

It doesn't give 2 different answers for me. I get 9 on both, which is correct for the code.

+2

ChaoticDawg your explanation about solving the problem is awesome. Thank you for guiding me towards right path.

+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.... :)

+1

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.

+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()

+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

+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; } //***************** }

0

ChaoticDawg did you understand my question?

0

Yes, write your code as I stated and it will be fixed.

0

ChaoticDawg my question is why the same code is giving two different answer in two different platforms?

0

ChaoticDawg can you run it on jsbin once?

0

Pavankumar AN I have and the output was 9, as I previously mentioned.

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

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.

0

Phil Andy Graves "on a potato" I really loled!

0

It doesn't give 2 different answers for me

0

i also tried , but till the answer came same.

0

function main(distance) { var depth = parseInt(distance); //your code goes here var day = 0; var total = 0; while(total<depth){ day = day + 1; total = total + 7; if(total >= depth){ console.log(day); break; } total = total - 2; } } main(32) main(51) its not like that which you ask but i just do for practice, nowdays i am taking preparation from https://www.dumpscollection.com/Platform-App-Builder-exam.html for platform-app-builder certification for promotion.

0

When two clearly different questions actually do have the same answer, then that answer should be allowed in both places. https://www.mybalancenow.work/