function fibonacci(max) { var result = [0, 1], val; for (var i = 1; result[i] < max; i++) { val = result[i] + result[i - 1] val <= max && result.push(val); } console.log(result); } fibonacci(100);