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

270
Views
Javascript: How do I use the same function with different parameters?

I would be forever grateful if you could enlighten me how to get it to work. Currently when I run it it gives the alert upon the first entered username.

let userName1;
let userName2;
let userName3;

alcoholAgeCheck();
alcoholAgeCheck(userName2);
alcoholAgeCheck(userName3);

function alcoholAgeCheck() {
  userName1 = prompt('Enter name');

  if (userName1 === userName2 || userName1 === userName3) {
    alert('Sorry, you may only use the same name once');
  } else if (userName2 === userName1 || userName2 === userName3) {
    alert('Sorry, you may only use the same name once');
  }
}

about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

Save all the previous inputs in a variable, and check whether the new input is in the set.

const userNames = new Set();

function alcoholAgeCheck() {
  userName = prompt('Enter name');
  if (userNames.has(userName)) {
    alert('Sorry, you may only use the same name once');
  } else {
    userNames.add(userName);
  }
}

alcoholAgeCheck();
alcoholAgeCheck();
alcoholAgeCheck();
console.log(Array.from(userNames));

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!