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

134
Visualizações
How to return a string with quotes on output

I wrote this code:

function unusedDigits(numbers) {
    let s = numbers.join('');
    let arr = s.split('');
    let ch = ['0', '1', '2', '3', '4', '5', '6', '7', '8', '9'];
    let arraRes = [];


    for (let i = 0; i < ch.length; i++){
        if (!arr.includes(ch[i])) {
           arraRes += ch[i];
        }
    }

    return String(arraRes); // also tried return arraRes + "";
}

console.log(unusedDigits([12, 34, 56, 78])); //09
console.log(unusedDigits([2015, 8, 26]));  //3479

But I can't understand why a number on output. I need on output numbers with quotes - "09" and "3479"

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

0

The following line:

arraRes += ch[i];

Converts the array into a string. You should use push() to add stuff to an array. This way the array is preserved and you're getting an array of string as return


function unusedDigits(numbers) {
    let s = numbers.join('');
    let arr = s.split('');
    let ch = ['0', '1', '2', '3', '4', '5', '6', '7', '8', '9'];
    let arraRes = [];


    for (let i = 0; i < ch.length; i++){
        if (!arr.includes(ch[i])) {
           arraRes.push(ch[i]);
        }
    }

    return arraRes;
}

console.log(unusedDigits([12, 34, 56, 78])); //09
console.log(unusedDigits([2015, 8, 26]));  //3479

about 4 years ago · Juan Pablo Isaza Relatório

0

Two possible ways:

Example with string simple concat

function unusedDigits(numbers) {
    let s = numbers.join('');
    let arr = s.split('');
    let ch = ['0', '1', '2', '3', '4', '5', '6', '7', '8', '9'];    
    let strRes= '';

    for (let i = 0; i < ch.length; i++){
        if (!arr.includes(ch[i])) {           
           strRes += ch[i] + ', ';
        }
    }
        return strRes;    
}

console.log(unusedDigits([12, 34, 56, 78])); //09
console.log(unusedDigits([2015, 8, 26]));  //3479

Example with array

function unusedDigits(numbers) {
    let s = numbers.join('');
    let arr = s.split('');
    let ch = ['0', '1', '2', '3', '4', '5', '6', '7', '8', '9'];
    let arraRes = [];

    for (let i = 0; i < ch.length; i++){
        if (!arr.includes(ch[i])) {
           arraRes.push(ch[i]);
        }
    }

    return String(arraRes); // also tried return arraRes + "";
}

console.log(unusedDigits([12, 34, 56, 78])); //09
console.log(unusedDigits([2015, 8, 26]));  //3479

Conclusion: I would prefer the first one because you dont need to cast the array back to an string.

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