Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

114
Views
triangle Number pattern using loop in javasctipt

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

about 4 years ago · Juan Pablo Isaza
3 answers
Answer question

0

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)

about 4 years ago · Juan Pablo Isaza Report

0

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))

about 4 years ago · Juan Pablo Isaza Report

0

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));

about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!