Why am I getting an "undefined" line after what I actually want?

4523 views javascript undefined
6

Spoiler warning: I am posting a solution for the problem "Staircase" of HackerRank. So, I am trying to solve a beginner's challenge, with JS to print a ladder. I have to write a function that prints a ladder according to the number of steps entered as a parameter. For example:

stairs(4)

Should return

   #
  ##
 ###
####

I got this code, and while it is mostly OK, I get an "undefined" line in the end. So:

function staircase(n) {

    var stair=[]; /*array of strings */

    for (var i=1; i <=n; i++){
        var step= " ".repeat(n-i).concat("#".repeat(i));
        stair.push(step);

    }
    for (var i=0; i<=stair.length; i++){
        console.log(stair[i])
    }
}

When staircase(6) is run, it returns this:

     #
    ##
   ###
  ####
 #####
######
undefined

What bothers me is the "undefined" word, that prevents me from submitting the slution. I only guessed a possible solution was replacing the "<=" in the 2nd for loop declaration, for simply "<", and that did it:

function staircase(n) {

    var stair=[]; /*array of strings */

    for (var i=1; i <=n; i++){
        var step= " ".repeat(n-i).concat("#".repeat(i));
        stair.push(step);

    }
    for (var i=0; i<stair.length; i++){
        console.log(stair[i])
    }
}

When I run staircase(6), it returns what it should:

     #
    ##
   ###
  ####
 #####
######

Although the problem is solved, I do not understand precisely why. My question is: Why does the "undefined" word appears in the first case, and why does the replacement I did fixes it? Thx!

answered question

Index which equals stairs.length doesnt exist.Last index in your array will be stairs.length -1 since you are starting from index number '0'.` i<= ` will work if you start from index 1 for(let i=1;i<=stairs.length;i++ .

4 Answers

9

Given an array of [1, 2, 3] it will have a length of 3.

  • foo[0] is 1
  • foo[1] is 2
  • foo[2] is 3

Since 3 <= array.length you then log foo[3] which is… undefined.

posted this
2

Word undefined appears because you are trying to access array element which doesn't exist.

console.log([][0], [][1], ['*'][0], [][1])

posted this
5

You should run on stair array till i < stair.length like this:

function staircase(n) {

  var stair=[]; /*array of strings */

  for (var i=1; i <=n; i++){
      var step= " ".repeat(n-i).concat("#".repeat(i));
      stair.push(step);

  }
  for (var i=0; i<stair.length; i++){
      console.log(stair[i])
  }
}

staircase(6)

Because Array starts from zero index, stair[stair.length] is undefined.

posted this
9

Your first loop runs from 1 to n(n iterations)

for (var i=1; i <=n; i++){
        // statements
}

while your second loop runs from 0 to n(total n+1 iterations)

for (var i=0; i<=stair.length; i++){
        console.log(stair[i])
}

Since stairs only have n values in it, on n+1th iteration, undefined is returned.

posted this

Have an answer?

JD

Please login first before posting an answer.