# Why the code is not giving desired result?

Tell us what’s happening:
Describe your issue in detail here.

So I want to print first n fibonacci numbers when i run the function fib(n). But in the output, it is returning the nth number twice even though i have provided the condition of i < num in the code below. Example if i run fib(5), the output comes as “1 1 2 3 5 5”.

``````  **Your code so far**
``````
``````
function fib(num) {
let arr = [1,1];
for(let i=2; i<num; i++){
arr[i] = arr[i-1] + arr[i-2];
arr.push(arr[i]);
}
return arr.join(" ");
}

console.log(fib(5));
``````
``````  **Your browser information:**
``````

User Agent is: `Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:91.0) Gecko/20100101 Firefox/91.0`

Challenge: Sum All Odd Fibonacci Numbers

Why are you adding the nth Fib number twice?

1 Like

Sorry didn’t get you here. Can you please elaborate. Has it something to do with my formula for arr[i] ?

This puts the `i`th Fib number into your array.

This puts the `i`th Fib number into your array again.