jeudi 23 avril 2015

JavaScript Countdown Loop

Im writing some JavaScript to take an array of 9 numbers 1-9 which are out of order, and return a countdown from 10 - 1 in a string. For example:

instructions = [4, 9, 3, 10, 6, 8, 2, 7, 1, 5];

And my javascript should return:

"10 9 8 7 6 5 4 3 2 1 liftoff!"

Here is the JavaScript test:

Test.assertEquals(liftoff([2, 8, 10, 9, 1, 3, 4, 7, 6, 5]),"10 9 8 7 6 5 4 3 2 1 liftoff!")

Here is the JavaScript:

function liftoff(instructions){

  var countdown = "";
  var start = 10;

  for (start; start >= 1; start--) {

    for (var i = 0; i < instructions.length; i++) {

      if (instructions[i] == start) {
        var count = instructions[i].toString();
        countdown += count + " ";
      }

    }

  }

  countdown += " liftoff!";
  console.log(countdown);
}

And here is the error I am getting:

Expected: 10 9 8 7 6 5 4 3 2 1 liftoff!, instead got: undefined

Why is it going undefined?

Aucun commentaire:

Enregistrer un commentaire