i have this but it can not resolve the problem:
for (let i = 1; i <= 5; i ) {
let line = '';
for (let j = 1; j <= 5 - i; j ) {
line = ' ';
}
for (let k = i; k <= 2 * i - 1; k ) {
line = `${i}` ' ';
}
for (let l = 2 * i - 2; l >= i; l--) {
line = `${l}` ' ';
}
console.log(line);
}
.as-console-wrapper { max-height: 100% !important; top: 0; }
This is the output I want:
1
2 2
3 4 3
4 5 5 4
5 6 7 6 5
It forms an equilateral triangle.
CodePudding user response:
linenumber = 5
for (let i = 1; i <= linenumber; i ) {
let line = ''
for (let space = 1; space <= linenumber-i; space ){
line = ' ';
}
for (let j = 1; j <= i; j ) {
let numberadd = Math.abs(j-1);
let numberadd2 = Math.abs(i-j);
if (numberadd > numberadd2){
numberadd = numberadd2
}
line = i numberadd ' ';
}
console.log(line)
}
.as-console-wrapper { max-height: 100% !important; top: 0; }
This is a very simple make a pyramid with an extra sauce. For every cell, you need to its value by closest distance relative to the 'walls'.