Jacks
Jacks

Reputation: 31

Why is this while loop skipped in javascript?

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

Upvotes: 2

Views: 106

Answers (5)

DragShot
DragShot

Reputation: 341

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!

Upvotes: 0

Naga Sai A
Naga Sai A

Reputation: 10975

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

Upvotes: 0

slebetman
slebetman

Reputation: 113896

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.

Upvotes: 4

Jesus is Lord
Jesus is Lord

Reputation: 15399

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

Upvotes: 0

Chris Zimmerman
Chris Zimmerman

Reputation: 331

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

Upvotes: 3

Related Questions