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

140
Visualizações
JavaScript pattern to display in browser

I am trying to make a Javascript pattern that accepts a user input in the form of a digit and returns the below output:

******1 
****1 2 1
***1 2 2 1
**1 2 3 2 1
*1 2 3 3 2 1
1 2 3 4 3 2 1
*1 2 3 3 2 1
**1 2 3 2 1
***1 2 2 1
****1 2 1
*****1

However I am stuck at the a pattern that looks like this:

         1
       1 2 1
     1 2 3 2 1
   1 2 3 4 3 2 1
     1 2 3 2 1
       1 2 1
         1

This is the code I wrote until now.

function selectNo() {
  var selectedNo = document.getElementById("select-number").value;
  for (var i = 1; i <= selectedNo; i++) {
    for (var l = 0; l < (selectedNo - i); l++)
      document.write("&nbsp;&nbsp; ")
    for (var j = 1; j <= i; j++)
      document.write(" " + j)
    for (var k = i - 1; k >= 1; k--)
      document.write(" " + k)
    document.write("<br>");
  }
  for (var i = 1; i <= selectedNo; i++) {
    for (var l = 0; l < i; l++)
      document.write("&nbsp;&nbsp; ")
    for (var j = 1; j <= (selectedNo - i); j++)
      document.write(" " + j)
    for (var k = selectedNo - i - 1; k >= 1; k--)
      document.write(" " + k)
    document.write("<br>");
  }
}
<input type="number" id="select-number" placeholder="Select your number">
<button onclick="selectNo()">Print the pattern</button>

Any suggestions on how to reach the target pattern? Thank you!

about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

Create a function that returns the array of the next numbers and then you can loop from 1 to the input number. Then you just need to join the arrays.

function line(len) {
    const middle = (len - 1) / 2;
    const a = [];
    for(let i = 0; i < len; i++) {
        if(i < middle) {
            a.push(i + 1);
        }
        else {
            a.push(len - i);
        }
    }
    return a;
}

function diamond(len) {
    l = len * 2 - 1;
    const d = [];
    for(let i = 0; i < l; i++) {
        const a = line(i + 1).map(v => `${v} `);
        const b = new Array(l - a.length).fill('&nbsp;').concat(a).join('');
        d.push(b);
    }
    for(let i = l - 2; i >= 0; i--) {
        const a = line(i + 1).map(v => `${v} `);
        const b = new Array(l - a.length).fill('&nbsp;').concat(a).join('');
        d.push(b);
    }
    return d.join('<br/>');
}

function main() {
  document.getElementById("result").innerHTML = diamond(document.getElementById("val").value);
}
<input id="val" type="number" placeholder="Select your number">
<button onclick="main()">Print the Pattern</button>
<div id="result" style="font-family: monospace;"></div>

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