Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

241
Visualizações
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 Respostas
Responde à pergunta

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 Relatório

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 Relatório

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 Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda