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

177
Views
How to exclude undefined parameters in a closure?

A JavaScript practice question on closures says the following:

Change the function to support multiple function calls:

function joinWords(a, b) {
    return console.log(a + ' ' + b);
}

See example cases below for example inputs and their expected outputs.

Input 1:
const greet = joinWords("Hello");
greet("world);
Output 1:
"Hello World"

Input 2:
const greet = joinWords("Hey", "there!");
greet("Where", "is", "Foo", "Bar?");
Output 2:
"Hey there! Where is Foo Bar?"

For the solution, I've successfully implemented a closure, but I'm still console logging undefined parameters. AKA, for the first test case (Input 1 and Output 1) I'm outputting Hello undefined World undefined undefined undefined. How can I elegantly exclude undefined parameters here?

function joinWords(a, b) {
    return function(c, d, e, f) {
    console.log(
        a + ' ' + b + ' ' + c + ' ' + d + ' ' + e + ' ' + f
    );
  };
}
about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

You might be looking for rest parameters, which make the function accept any number of arguments and passes them over as an array (which one can then call the .join method on):

 function joinWords(...values) {
   return values.join(" ");
 }
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!