2

I'm trying to create a function that returns a "staircase". The output needs to look like this:

  var staircase5 = [
        "    #",
        "   ##",
        "  ###",
        " ####",
        "#####"
    ]

The code I've written so far is:

function staircase (valueA){
    const array1 = [];
    for (let i = 0; i < valueA; i++){
        let cicle = valueA[i];
        let step = " #".repeat(cicle);
        array1.push(step)
    }
console.log(array1);
}

When I run the code in VS Code, console.log(staircase(3)); , this is the result:

[ '', '', '' ]
undefined

Any clue as to why it is returning the strings as ' ' instead of '#'? And why is it not really repeating? Thanks a lot!

14
  • Where do you call your function? Commented Jul 13, 2020 at 18:53
  • 3
    @VLAZ: Then his valueA[i] would seem out of place. Commented Jul 13, 2020 at 18:54
  • 1
    If you are passing 3, then valueA is 3, which is not an array or array-like object, so passing an index to it valueA[i] returns undefined. If you open your developer tools and look at the console, you'll see an error about it. Commented Jul 13, 2020 at 18:56
  • 1
    As I said, you can't pass an index to a number. Just i will give you the number of the current loop iteration, not valueA[i]. Commented Jul 13, 2020 at 19:00
  • 1
    @DominikMatis: Not really, hev1's answer works better for me. Thanks a lot anyways. Commented Jul 13, 2020 at 19:04

3 Answers 3

1

You are currently trying to treat valueA, which is a number, like an array. If you loop from 1 to valueA, then each step should be the space repeated value - i times concatenated with the # character repeated i times.

function staircase (valueA){
    const array1 = [];
    for (let i = 1; i <= valueA; i++){
        let step = " ".repeat(valueA - i) + "#".repeat(i);
        array1.push(step)
    }
    return array1;
}
console.log(staircase(5))

Sign up to request clarification or add additional context in comments.

2 Comments

This is working for me! Thanks a lot. As soon as I can accept the answer I will.
@LuisArgüelles No problem.
0

You need to use padding.

const staircase = count => {
    let array1 = [];
    let step = ""
    for (let i = 0; i < count; i++){
        step = "#".repeat(i+1);
        array1 = [...array1, step.padStart(count, ' ')]
    }
    console.log(array1);
}

staircase(5)

2 Comments

Read the comments under the question. The OP is trying to pull an index of a number. You've provided a working answer without answering the question as to what is wrong.
"I've updated the question to show how I'm calling the function: console.log(staircase(3)); – Luis Argüelles 3 mins ago" ... really?
0

If you need your output look like this:

  var staircase5 = [
        "    #",
        "   ##",
        "  ###",
        " ####",
        "#####"
    ]

Your function should be like this:

function staircase (valueA){
    const array1 = [];
    for (let i = 1; i <= valueA; i++){
        let step = " ".repeat(valueA-i)+"#".repeat(i);
        array1.push(step)
    }
  return array1
}

Explanation

In your function:

 function staircase (valueA){
     const array1 = [];
     for (let i = 0; i < valueA; i++){
         let cicle = valueA[i];
         let step = " #".repeat(cicle);
         array1.push(step)
     }
     console.log(array1); 
  }

I supposse you are calling the function like this:

var staircase5 = staircase(5);

In this line: let cicle = valueA[i]; you are trying to take each value of valueA (as an array) and in this line: for (let i = 0; i < valueA; i++) you are using it as a number.

and in this line: let step = " #".repeat(cicle); if your valuaA is equal to 5 your output will be:

[
   " # # # # #",
   " # # # # #",
   " # # # # #",
   " # # # # #",
   " # # # # #"
]

In my solution I use i in " ".repeat(valueA-i)+"#".repeat(i); to iterate between the values that you need to make the array.

When i increment " ".repeat(valueA-i) will render less spaces. (ie: if i=2 and valueA=5, step will be: " ", 3 spaces)

Test:

function staircase (valueA){
    const array1 = [];
    for (let i = 1; i <= valueA; i++){
        let step = " ".repeat(valueA-i)+"#".repeat(i);
        array1.push(step)
    }
  return array1
}

var staircase5 = staircase(5);

console.log(staircase5)

1 Comment

A good answer is more than just posting working code. You should explain the problem and how your solution solves it so that others can learn something.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.