2

In both conditions the while loop should evaluate to true. The loop variable is variable is set to be less than h and greater than zero.

function snailClimb(){
var h = 6;
var u = 3;
var d = 1;
var f = 10;
var result = 0;
var dayTravel;
var container = [];
var initialDay = u - d;
container.push(initialDay);
var travel = u;
var totalDistance;
totalDistance = container.reduce((prev, curr) => prev + curr ); 
while( totalDistance < h && totalDistance > 0) { // BEFORE IT WAS || instead of &&

    dayTravel = travel - (travel * (f/100));
    if (dayTravel !== 0){
    container.push(dayTravel);
    }
    travel = dayTravel;
    container.push(-d);
    result++;
    totalDistance = container.reduce((prev, curr) => prev + curr ); // this was added as well.

}

console.log(totalDistance);
console.log(result);
}

snailClimb();
5
  • 4
    totalDistance is never updated inside the loop so once it enters the condition will always stay true Commented Apr 30, 2017 at 23:01
  • Thanks Bassie. I tried that but then the entire while loop gets skipped. Commented Apr 30, 2017 at 23:03
  • 2
    @Jacks How can changing code inside the loop prevent it from being entered? If you didn't change code inside the loop, you didn't "try" what Bassie is talking about. Commented Apr 30, 2017 at 23:07
  • You can't JUST evaluate totalDistance inside the loop; you still have to do it before, but also inside. Commented Apr 30, 2017 at 23:14
  • Please don't change the logic of the question after people have started to answer. All the answers below now does not make any sense at all and people who encounter while loop being skipped in the future will be very confused with the accepted answer. Remember, SO is not a forum. It is a database of solutions to programming problems. You are not merely asking us to solve your problem. You are formulating a programming problem to help other people get answers. Commented Apr 30, 2017 at 23:19

5 Answers 5

4

There are two reasons why this is an infinite loop.

  1. You never update totalDistance. In the code totalDistance is the same value thus if it's true the first time it will always be true.

  2. Consider the logic of your while condition:

    while (totalDistance < 6 || totalDistance > 0) { ...

If totalDistance is more than 6 (the value of h) then it will evaluate to:

while (false || true) ...

Which is the same as: while (true) ..

If totalDistance is less than 0 then it will evaluate to:

while (true || false ) ...

which is the same as while (true) ..

If totalDistance is between 6 and 0 then it will evaluate to:

while (true || true) ...

So as you can see, there is no condition where the while loop will terminate.

What you probably want is:

while (totalDistance < h && totalDistance > 0) { ...

The English language is unfortunately very lazy about its logical usage of and and or. In English it's perfectly normal for people to use the word or when they mean logical and. Be very careful of thinking in English. When programming you need to think Methamatically.

Sign up to request clarification or add additional context in comments.

Comments

3

totalDistance never changes inside the loop. Therefore, the condition totalDistance < h || totalDistance > 0 will never have a different evaluation.

1 Comment

@Jacks: That is impossible; in order to execute the code that was changed, the loop had to have been entered.
0

Your loop checks the values totalDistance and h.

However your loop only updates dayTravel, container, travel, result.

You need to update totalDistance or h to exit the loop (due to the || you need to update totalDistance, really).

Comments

0

To acheive expected result , use below option

1.Important thing to remember using 'while' is to make sure that there is some parameter which make its condition false

2.Modified the condition such that evaluates result to h and executes the while loop successfully

while( result < h)

JS:

    function snailClimb(){
    var h = 6;
    var u = 3;
    var d = 1;
    var f = 10;
    var result=0;
    var dayTravel;
    var container = [];
    var initialDay = u - d;
    container.push(initialDay);
    var travel = u;
    var totalDistance=0;

      while( result < h) {

        dayTravel = travel - (travel * (f/100));
        if (dayTravel !== 0){
        container.push(dayTravel);
        }
        travel = dayTravel;
        container.push(-d);
        result++;
        totalDistance = container.reduce((prev, curr) => prev + curr );
    }


    console.log("total Distance",totalDistance);
    console.log("result", result + 1);
    }

    snailClimb();

codepen url for reference- https://codepen.io/nagasai/pen/RVVLvd

Comments

0

The loop variable is variable is set to be less than h and greater than zero.

Ugh... then why on Earth did you write this?:

while( totalDistance < h || totalDistance > 0) {

Wouldn't this make more sense?

while (totalDistance < h && totalDistance > 0) {

That aside, you are evaluating totalDistance and h in order to get in the loop, but they are never even touched inside of it. Of course the result will be either an infinite loop or a no-op. "Not big surprise", said the Heavy.

My suggestion? Go back to the draft or diagram (in case you have any) and think again about what variables you need and how will use use them. This is a mess, and as far as I know, that's the only way to fix it. At least that's what worked when I had to fix my own mess back when I was starting with things.

Don't worry too much, though. With some more attempts I'm sure you should be able to code what you really intend to do. Good luck!

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.