Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

115
Visualizações
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 Respostas
Responde à pergunta

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 Relatório

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 Relatório

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 Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda