Hi there, I'm currently facing an issue. I am trying to generate a triangular number pattern like the one shown below:
Output:
1223334444333221
=22333444433322=
===3334444333===
======4444======
I attempted to write a program for this, however, the logic I implemented is not working as expected.
function challengeAttempt(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(challengeAttempt(4))
If anyone can assist me with finding a solution, I would greatly appreciate it. Thank you!