Good evening and as always, thanks in advance. I did an exercise to write with asterisks in a pyramid shape (I explain it better in this code snippet)
function pyramid(numPisos) {
for (let i = 0; i < numPisos; i++) {
let piso = '';
for (let j = 0; j < i + 1; j++) {
piso = piso + '*';
}
console.log(piso)
}
}
pyramid(5);
and the next thing I want to do is make this same pyramid right-aligned, instead of left-aligned as it is now. I know I need another for* loop between the two existing ones, but after trying more than 100 ways the only thing I get is that the pyramid has 5 spaces on each line before writing the asterisk. That is, I can only move it to the right but not Change your lineup. In case I give rise to many doubts, my goal would be to leave it like this:
*
**
***
****
*****
Thank you very much.
Following your example, you can do it as you mention, adding another for.
The code would be as follows:
certainly one of the solutions is to add a third loop to determine the number of blank spaces that must exist to move the asterisk to the right and therefore get the pyramid to be aligned to the right, it is worth mentioning that the result will look good as long as it is printed using monospaced font (like browser console). I attach the code with the solution to your question. Cheers
You can do it with a for, a javascript method that is responsible for repeating the empty string with padStart then concatenates the other code with the amount of *
we change the order of the chain to generate the tree, I invite you to play with this method
for()