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

240
Views
How to add a comma separator after each params if param has values and dot in the end of params

for example we have 3 params

function test(a,b,c){
  console.log(a + ',' + b + ',' + c +'.')
}

test('a','b','c')

will print

> a, b, c.

but in case b will be empty, the result will look with two comma, like

test('a','','c')

will print

a,,c.

we can improve like checking each var like this

function test(a,b,c){
      console.log(a + (a?',':'') + b + (b?',':'') + c +(c?'.':''))
    }

so now

 test('a','','c')

will print

a,c.

look ok, but when we have only b

test('','b','')

will print

  b,

and now we must to check values a or b exist and there is no c to print '.'

and complexity increases, but what if we have n vars, any ideas how to solve more easily

expected result is :

test('a','b','c') => a, b, c.
test('a','b','') => a, b.
test('a','','') => a.
test('','b','c') => b, c.
test('','b','') => b.
test('','','') => 
about 4 years ago · Juan Pablo Isaza
3 answers
Answer question

0

You can filter and use the ...rest parameter

const isParm = prm => prm !== "" && prm !== undefined && prm !== null;

const test = (...theArgs) => {
  const list = theArgs.filter(parm => isParm(parm)); 
  if (list.length > 0) console.log(`${list.join(",")}.`)
}

let x; 
test('a','b','c')
test('a',null,'c'); // null is a falsy value
test('','','c')
test('a')
test('','b')
test(x); // undefined (falsy)
test(0,0,0); // falsy values

about 4 years ago · Juan Pablo Isaza Report

0

If I understand your intention, this should do it.

function test(...args) {
  const res = args.filter(i => i).join(',')
  return res + (res && ".")
}


// Usage
console.log(test('a','','c')); // "a,c."
console.log(test('a','b','c')); // "a,b,c."
console.log(test('','','c')); // "c."
console.log(test('a', '', '')) // "a."
console.log(test('', '', '')) // ""

about 4 years ago · Juan Pablo Isaza Report

0

Using an array join approach, we can add all inputs to an array. Then filter off empty entries and join by a comma separator.

function test(a,b,c) {
    var array = [a, b, c];
    return array.filter(x => x).join(", ") + ".";
}

console.log(test('a','','c'));

Actually, a better function signature for your function would accept an array, which allows for concat with separator of any number of items.

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!