Why is the while condition not an infinite loop?

Asked

Viewed 47 times

-4

Why the condition of this while is not a loop infinite?

function fibonacci(n) {


const fibSequence = [1];

let currentValue = 1;
let previousValue = 0;

if (n === 1) {
  return fibSequence;
}

let iterationsCounter = n - 1;

while (iterationsCounter) {
  currentValue += previousValue;
  previousValue = currentValue - previousValue;

  fibSequence.push(currentValue);

  iterationsCounter -= 1;
}

return fibSequence;
}
  • Did the answer solve your question? Do you think you can accept it? See [tour] if you don’t know how you do it. This would help a lot to indicate that the solution was useful for you. You can also vote on any question or answer you find useful on the entire site (when you have 15 points).

1 answer

1

Javascript is a weak and dynamic typing language. This means that it makes value coercion whenever it is necessary to give a valid result.

That’s what gives the language a bad name, if it didn’t have the weak JS typing would be a language much closer to being very good. It gives an unnecessary facility for the person to learn the basics and use in a way apparently a little easier, but soon after you start doing more complex things it shows a huge problem, which becomes a joke at various times between developers. But look, this wasn’t supposed to be a problem, the language was created to make something very simple and give a little animation or do a simple action on a very simple web page. The problem was that they started making complex code on a single page which is a true application. The language is not suitable for this.

So whenever a site requires a boolean value of false or true, which is the case of a if or a while, the ideal is to generate a boolean value. But within the philosophy of weak typing JS, it makes any value a Boolean in some way, that is, it uses some rule of its own to convert another value, for example a number, into a Boolean. And it all works.

In that case there is a value in iterationsCounter which is considered a value Truthy, that is, it is true, because very few values are considered false. When this variable reaches a value Falsy the while turns fake and ends.

False values, according to the link above, are: false, 0, -0, 0n, null, undefined, NaN. So when the variable reaches the value 0 the condition is false. would be the same as saying more explicitly: iterationsCounter != 0.

The fact that bad choices have been made helps to give some problems, but not in this case. I wouldn’t even say that this shape is much less readable or cause so much confusion. Cause any.

That code has at least one bug. If the value is 0 or negative it does not work:

function fibonacci(n) {
    const fibSequence = [1];
    let currentValue = 1;
    let previousValue = 0;
    if (n === 1) return fibSequence;
    let iterationsCounter = n - 1;
    while (iterationsCounter) {
      currentValue += previousValue;
      previousValue = currentValue - previousValue;
      fibSequence.push(currentValue);
      iterationsCounter -= 1;
    }
    return fibSequence;
}

fibonacci(0);

I put in the Github for future reference.

It would be good to test this before reaching the loop and deciding what to do, probably not even returning a valid result.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.