Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

133
Vistas
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 Respuestas
Responde la pregunta

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 Denunciar

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 Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda