Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

208
Views
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 answers
Answer question

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 Report

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 Report

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 Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!