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

214
Vistas
How can I make my console print the values as opposed to showing undefined
const howLong = 5
let special = [
    "!",
    "@",
    "#",
    "$",
    "%",
    "+",
    "&",];

let finalPassword = []

for (let i = 0; i < howLong; i++) { 
    finalPassword += special[Math.floor(Math.random() * howLong)].push
    
}

console prints undefined 5x, my goal is to make it copy 5 random characters from the "special" array into a new var "finalPassword"

about 4 years ago · Juan Pablo Isaza
3 Respuestas
Responde la pregunta

0

you should remove the .push call

const howLong = 5;    
let special = [
        "!",
        "@",
        "#",
        "$",
        "%",
        "+",
        "&",];
    
    let finalPassword = "";    
    for (let i = 0; i < howLong; i++) { 
        finalPassword += special[Math.floor(Math.random() * howLong)]
    }
about 4 years ago · Juan Pablo Isaza Denunciar

0

Depending on what your expected output requirements are you can either concatenate "special" characters on to a string:

const howLong = 5;

let special=["!","@","#","$","%","+","&"];

let finalPassword = '';

for (let i = 0; i < howLong; i++) { 
  const rnd = Math.floor(Math.random() * howLong);
  finalPassword += special[rnd];
}

console.log(finalPassword);

...or you can push those "special" characters into an array (and then maybe join up that array of elements into a string).

const howLong = 5;

let special=["!","@","#","$","%","+","&"];

let finalPassword = [];

for (let i = 0; i < howLong; i++) { 
  const rnd = Math.floor(Math.random() * howLong);
  finalPassword.push(special[rnd]);
}

console.log(finalPassword);
console.log(finalPassword.join(''));

But you can't do both operations at the same time.

about 4 years ago · Juan Pablo Isaza Denunciar

0

In the for loop iteration, generate a random index and then access the special array in that index to fetch the special character.

const howLong = 5;

let special = [
    "!",
    "@",
    "#",
    "$",
    "%",
    "+",
    "&",];

let finalPassword = []

for (let i = 0; i < howLong; i++) { 
    const currentRandom = Math.floor(Math.random() * howLong);
    finalPassword.push(special[currentRandom]);
}

console.log(finalPassword);

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