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)
valueA[i]would seem out of place.3, thenvalueAis3, which is not an array or array-like object, so passing an index to itvalueA[i]returnsundefined. If you open your developer tools and look at the console, you'll see an error about it.iwill give you the number of the current loop iteration, notvalueA[i].