0

        var firstNum = 1;
        var secondNum = 2;
        var fibonacciNum;
        var myArray = [];

        for (var k = 3; k <=15; k++) {

            fibonacciNum = firstNum + secondNum;
            firstNum = secondNum;
            secondNum = fibonacciNum;
            
            myArray.push(fibonacciNum);

        }

im trying to push the numbers from the loop into myArray and later putting each number as a list item in an unordered list

2 Answers2

0

Replace

//fibonacciNum.push(myArray.length);

with

myArray.push(fibonacciNum);
yas
  • 3,520
  • 4
  • 25
  • 38
0

So, assuming you want to add a new Unordered list to the DOM: (this will work for any array...)

function addUl(arr) {
    //A <ul> is created and appended to the DOM
    var ul = document.createElement("ul");
    ul.setAttribute("id", "list");
    document.body.appendChild(ul);

    var li;
    var element;
    //A <li> is created and filled with an element each time
    //the element will change to the next array element every lap (a)
    arr.forEach(function (a) {
        li = document.createElement("li");
        element = document.createTextNode(a);
        li.appendChild(element);
        document.getElementById("list").appendChild(li);
    });
}

here you can se how it works: example

By the way, if you use jQuery your life will be easier...

Andres C. Viesca
  • 332
  • 5
  • 19