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

182
Visualizações
Array.join('') is not working as it should inside of my recursive function

I am attempting to use a recursive approach in order to write a function that will take in a binary string, break it apart into 8 bit sections, and return the string translated to ASCII. As far as I can tell from the running debugger, my function should be working. The only problem I'm running into is that when I call join('') on my 'currentArray' variable, it is not in fact joining the array. Any input would be helpful into what I am doing wrong here.

function binaryToString(binaryBlob, finalString = []) {
      if (binaryBlob.length === 0) return finalString.join('');
    
      let currentArray = [];
      let binaryArray = binaryBlob.split('');
    
      currentBit = binaryArray.splice(0, 8);
    
      currentArray.push(currentBit);
        
      let joinedString = currentArray.join('');
      let base10String = Number(joinedString);
      let ASCII = String.fromCharCode(base10String);
      finalString.push(ASCII);
    
      return binaryToString(binaryArray.join(''), currentArray, finalString);
    };

Expected outputs:

console.log(binaryToString('010000010100001001000011')); // should return 'ABC'
console.log(binaryToString('001101100011011100111000')); // should return '678'
about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

A couple things:

  • Having variables properly named after their type helps for clarity. Name an array finalArr instead of finalString, etc.
  • currentArray doesn't seem to be necessary. Use slice directly on the string to get the first 8 characters for the byte, and slice again to provide the remaining characters to the next call.

Here is a slightly refactored version that makes it clearer:

function binaryToString(binaryStr, asciiArr = []) {
  if (binaryStr.length === 0) {
    return asciiArr.join('');
  }

  const byteStr = binaryStr.slice(0, 8);
  const remaining = binaryStr.slice(8);
  
  const byte = parseInt(byteStr, 2); // `2` indicates base of input string
  const ascii = String.fromCharCode(byte);
  asciiArr.push(ascii);

  return binaryToString(remaining, asciiArr);
};

console.log(binaryToString('010000010100001001000011')); // 'ABC'
console.log(binaryToString('001101100011011100111000')); // '678'
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