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
obtain from a matrix where the 0 will be space and if X is repeated

I need to understand how with an array of numbers, divide it into blocks when the number zero appears, this marks the beginning and end of this and another block.

Which I must sort in ascending order and that when printing the sequences must be separated by a space where the zero was and if there is a sequence of zeros, X would be printed.

Example 1: [1,3,2,0,7,8,1,3,0,6,7,1]. The result: 123 1,378 167 Example 2: [2,1,0,0,3,4]. The result: 12X34.

var myArray = [1,3,2,0,7,8,1,3,0,6,7,1] 
    
let salida ='';
let salida_temp = []

for(let i = 0; i < array.length; i++){
    if(array[i] !== 0 && i+1 < array.length)
       salida_temp.push(array[i])
    else{ 
       if(i+1 == myArray.length){
         salida_temp.push(myArray[i])
   }

Tank's

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

0

You have to go through series of consecutive steps to achieve the desired result.

function separateNumber(arr) {
  return arr
    .join("")
    .replace(/(0)\1+/g, "0X0")
    .split("0")
    .map((s) => s.split("").sort().join(""))
    .join(" ")
    .replace(/ X /g, "X");
}

console.log(separateNumber([1, 3, 2, 0, 7, 8, 1, 3, 0, 6, 7, 1]));
console.log(separateNumber([2, 1, 0, 0, 3, 4]));
console.log(separateNumber([2, 0, 1, 0, 0, 3, 4]));
console.log(separateNumber([2, 1, 0, 0, 3, 4, 0, 0, 0]));

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