Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

123
Vistas
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 Respuestas
Responde la pregunta

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 Denunciar

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 Denunciar

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 Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda