JavaScript How to limit Fibonacci Sequence in an array under a Number Javascript limit fibonacci sequence

I wrote a Fibonacci Sequence javascript solution. I was able to push out a Fibonacci Sequence array. However, this solution add an extra number in the array that is more than the limited number.

For example, if I want all the numbers in the array to be under 10, then the array should be [0, 1, 1, 2, 3, 5, 8]. However, with my solution, it still output the extra "13" in the array.

function fib(num) {
var x = 0;
var y = 1;
var fibarray = [0, 1];

while(y < num) {
    y += x;
    x = y - x;
    fibarray.push(y);
}

return fibarray;
}
console.log(fib(10));
Answer:1

It is happening because you are computing next y, pushing it to array and then checking if y is less than num or not but by then you have already added y to fibarray. Solution would be to check if y is less than num and then push it to fibarray and then compute next y and while condition is checked all over again.

function fib(num) {
var x = 0;
var y = 1;
var fibarray = [0];

while(y < num) {
    fibarray.push(y);
    y += x;
    x = y - x;
}

return fibarray;
}
console.log(fib(10));
Answer:2

...Or you can check that y+x < 10 not just y < 10

function fib(num) {
var x = 0;
var y = 1;
var fibarray = [0, 1];

while(y+x < num) {
    y += x;
    x = y - x;
    fibarray.push(y);
}

return fibarray;
}
console.log(fib(10));
Answer:3

It adds 13 because you modify y after comparing to num. A solution would be:

function fib(num) {
  var x = 0;
  var y = 1;
  var fibarray = [0, 1];

  while(true) {
    y += x;
    x = y - x;
    if(y>num) return fibarray;
    fibarray.push(y);
  }
}
Answer:4

function fib(num) {
var x = 0;
var y = 1;
var fibarray = [0,1];

while(y < num) {
    y += x;
    x = y - x;
    if(num < y) {
   return fibarray;
     }
   fibarray.push(y);
}


}
console.log(fib(10));
Answer:5

If I am understanding your question correctly, the reason for this is that "while(y < num)" lopps until you encounter a number greater than 10. As "fibarray.push(y);" is at the end of your loop, this means that when you get 8, it goes back into the loop once more. You don't have anything stopping it from pushing the 13 at that time.

Two options you have are to either include an if statement before the push statement (but after "x = y - x;") that says to exit the loop (e.g. "if(y >= 10){break;}"), or you can set the loop to be for numbers less than 8.

Answer:6

So we know that the action attribute used for sending the form data to a web server. But I have a couple of questions: If the action attribute is not set, why does it reload the page by default? ...

So we know that the action attribute used for sending the form data to a web server. But I have a couple of questions: If the action attribute is not set, why does it reload the page by default? ...

  1. form without action php
  2. form without action html
  3. form without action javascript
  4. form without action attribute
  5. form without action submit
  6. form without action
  7. form without action and method
  8. form without action parameter
  9. form action without redirect
  10. form action without reloading page
  11. form action without submit button
  12. form action without refresh
  13. form action without leaving page
  14. form action without button
  15. form tag without action
  16. form post without action
  17. html5 form without action
  18. html form without action attribute
  19. submit form without action javascript
  20. form tag without action attribute

So I'm new to react and I'm trying to understand how non-react code and react code can interact. So for example, let's say I have a library which draws a circle in a DOM element with syntax like this:...

So I'm new to react and I'm trying to understand how non-react code and react code can interact. So for example, let's say I have a library which draws a circle in a DOM element with syntax like this:...

So I more than one dynamicly generated elements with the same class name that I am trying to check input for in jQuery. Instead of it letting me click on both, it is just letting me click the first ...

So I more than one dynamicly generated elements with the same class name that I am trying to check input for in jQuery. Instead of it letting me click on both, it is just letting me click the first ...

  1. class click not working jquery
  2. class selector not working jquery
  3. remove class not working jquery
  4. jquery addclass not working
  5. jquery toggleclass not working
  6. has class not working jquery

I wrote a jQuery function to add and remove some input, but I got this error, pls help me ! I using jQuery 3.2.1 and Laravel 5.4 Uncaught TypeError: Cannot read property 'remove' of undefined Here ...

I wrote a jQuery function to add and remove some input, but I got this error, pls help me ! I using jQuery 3.2.1 and Laravel 5.4 Uncaught TypeError: Cannot read property 'remove' of undefined Here ...