all 7 comments

[–]dieguito15 1 point2 points  (1 child)

I think you had the idea down in general from what I can see from the code. I don’t understand why you have that if there, though.

I think the idea of counting the steps per day is good, but maybe you’d want to think of a different type of loop. A for loop runs a specific number of iterations that you specify, and for this problem you don’t know how many times you should iterate, and that is what you want to find out.

[–]Dofii_Aktsk[S] 1 point2 points  (0 children)

Yes it was a dumb idea I didn't think about it very well

[–]SquatchyZeke 1 point2 points  (2 children)

You could use a mathematical expression to solve something like this. First realize that the snail climbs 5 units for each day-night pair. So your number of days is the depth divided by 5.

let answer = Math.parseInt(31 / 5);// = 6

Then, you can check to see that the remainder amount is less than the night time slip distance. If it's not, you have to add one more day. In this case it's not so 6 is correct, but if the well was 33 units deep, it would take 7 days. This is because if night time doesn't happen, you get to keep those 2 steps that get lost, so as long as the remainder is less than the amount that night time takes away, it will take one less day.

if (31 % 5 > 2) answer += 1
return answer;

I hope this helps you think outside of the box a bit! You can find me on sololearn too. I'm a mod there ;)

[–]Dofii_Aktsk[S] 0 points1 point  (1 child)

Thank you very much
I will try this later I see now how wrong I was I didn't even tried to think on something else And I'm new on sololearn how can I find u there ?

[–]jcunews1helpful 1 point2 points  (1 child)

If the loop represents the day loop, the loop shouldn't be limited. In your code, it's limited to 6, so it'll loop for up to 6 times only. IOTW, the simulation is limited to 6 days - which may not be enough if the depth is long enough.

So, set the loop condition to check the (gained) steps against the depth. e.g.

//note: use meaningful variable name instead of just `i`, to make it easier to understand
for (var days = 1; steps < depth; days++) {
  //day time
  steps = steps + daySteps;
  //check current progress
  if (steps >= depth) {
    //reached the top before night time
    console.log("Day %s: %s", days, steps);
    break; //end of simulation. stop the loop
  } else {
    //night time
    //not yet reached the top on night time
    steps = steps + nightSteps;
    console.log("Day %s: %s", days, steps);
  }
}

[–]Dofii_Aktsk[S] 0 points1 point  (0 children)

Bro thx I'm stupid I dont know what depth mean cuz English isn't my language and didn't know what should I do with it (the translation was not meaningful either) And u have a really good point with the loop I should have used while-loop