Hello I'm having a problem. I want to create a triangular number pattern as follows:
Output:
1223334444333221
=22333444433322=
===3334444333===
======4444======
I've tried to make the program. But the logic I use is not quite right.
function nomor4(level) {
let len = null;
let result = [];
while (level > 0) {
let arr = [];
for (let i = 1; i < level; i++) {
for (let repeat = 0; repeat <i; repeat++){
arr.push(i)
}
}
// convert arr.push value from array to string using join
//and add 1 and the copy value using reverse
let str_level = arr.join("") + "4444" + arr.reverse().join("");
if (len == null) {
len = str_level.length;
}
//Add Strip
while (str_level.length < len) {
str_level = "-" + str_level + "-";
}
result.push(str_level);
level--;
}
return result.join("\n");
}
console.log(nomor4(4))
if anyone can. please help me give the solution. Thank you