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

218
Visualizações
fill blank with elements of array

I want to fill the '_' with reference array :

const reference = ['this is', 'a beautiful', 'car'];
const blank = ['I know', '_'];


console.log(fillBlank(reference))

function fillBlank(reference) {

    const ref = reference.join(' ');

    if(blank.length) {
    return // I know this is a beautiful car
  } else {
    return ref;
  }
  
}

Note that we may have blank in different places but we only have one in each blank array:

const blank = ['I know', '_'];
const blank = ['_', 'I know'];
const blank = ['I know', '_', 'right?'];

How can I do this?

about 4 years ago · Juan Pablo Isaza
3 Respostas
Responde à pergunta

0

You can map and join

const reference = ['this is', 'a beautiful', 'car'];
const ref = reference.join(' ');

const fillBlank = (blank,ref) => blank.map(item => item === "_" ? ref : item).join(", ")


console.log(fillBlank(['I know', '_'],ref))
console.log(fillBlank(['_', 'I know'],ref))
console.log(fillBlank(['I know', '_', 'right?'],ref))

We can take this a little step further and uppercase the first letter of the ref if needed

const reference = ['this is', 'a beautiful', 'car'];
const ref = reference.join(' ');

const fillBlank = (blank,ref) => blank.map((item,i) => item === "_" ? i>0? ref : ref.slice(0,1).toUpperCase() + ref.slice(1) : item).join(", ")


console.log(fillBlank(['I know', '_'],ref))
console.log(fillBlank(['_', 'I know'],ref))
console.log(fillBlank(['I know', '_', 'right?'],ref))

about 4 years ago · Juan Pablo Isaza Relatório

0

Try this instead of the 'return // I know this is a beautiful car':

return blank.map(item => item === '_' ? ref : item).join(' ')
about 4 years ago · Juan Pablo Isaza Relatório

0

function fillBlank(arrToReplace, reference) {
    const concatedRef = reference.join(' ');
    arrToReplace.splice(arrToReplace.findIndex(el => el === '_'), 1, concatedRef);
    return arrToReplace.join(' ');
}
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