Home > Software design >  triangle Number pattern using loop in javasctipt
triangle Number pattern using loop in javasctipt

Time:08-22

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

CodePudding user response:

Here's a way of doing this by using two nested maps over arrays of rows (a counter for each row) and columns (the values to print in each row)

const size = 4
const fill = '='
const rows = Array.from({length: size}, (_, i) => i   1) // 1,2,3,4
const cols = rows.concat(rows.slice(0, -1).reverse())    // 1,2,3,4,3,2,1

const result = rows
  .map(r => cols
    .map(c => ((c >= r) ? c : fill).toString().repeat(c)
    ).join('')
  ).join('\n')

console.log(result)

CodePudding user response:

There are better ways than this, but I will put this here just to show how to do it with minimal changes from the OP (I just changed - to = and the for loop with i).

function nomor4(level) {
  let len = null;
  let result = [];
  while (level > 0) {
    let arr = [];
    for (let i = 5-level; i < 4; 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))

CodePudding user response:

function nomor4(level) {
  let realValue = level;
  let len = null;
  let result = [];
  while (level >= 0) {
    let arr = [];
    for (let i = 1; i <= realValue; i  ) {
      if (realValue !== i) {
        for (let repeat = 0; repeat < i; repeat  ) {
          if (realValue - level <= i) {
            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);
    result = [...new Set(result)];
    level--;
  }
  return result.join("\n");
}

console.log(nomor4(4));

  • Related