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

510
Visualizações
How to make abbreviations/acronyms in JavaScript?

new to coding I'm trying to make a function that makes "abbreviations/acronyms" of words, e.g. 'I love you' -> 'ily'. I've tried rewriting the code in many ways but console.log only shows me the first letter of the first given word.

function makeAbbr(words) {
  let abbrev = words[0];
  let after = 0;
  let i = 0;
  for (const letter of words) {
    if (letter === '') {
      i = words.indexOf('', after);
      abbrev += words[i + 1];
    }
    after++;
  }
  return abbrev;
}
const words = 'a bc def';
let result = makeAbbr(words);
console.log(result)

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

0

Without using arrays. But you really should learn about them.

  1. Start by trimming leading and trailing whitespace.
  2. Add the first character to your acronym.
  3. Loop over the rest of the string and add the current character to the acronym if the previous character was a space (and the current character isn't).

function makeAbbr(words) {
  words = words.trim();
  const length = words.length;
  let acronym = words[0];

  for(let i = 1; i < length; i++) {
    if(words[i - 1] === ' ' && words[i] !== ' ') {
       acronym += words[i];
    }
  }

  return acronym;
}
console.log(makeAbbr('I love you'));
console.log(makeAbbr('I     love     you'));
console.log(makeAbbr('   I    love    you   '));


And here's the version for GottZ

function w(char) {
   char = char.toLocaleLowerCase();
   const coll = Intl.Collator('en');
   const cmpA = coll.compare(char, 'a');
   const cmpZ = coll.compare(char, 'z');

   return cmpA >= 0 && cmpZ <= 0;
}

function makeAbbr(words) {
  words = words.trim();
  const length = words.length;
  if(!length) return '';

  let acronym = words[0];

  for(let i = 1; i < length; i++) {
    if(!w(words[i - 1]) && w(words[i])) {
       acronym += words[i];
    }
  }

  return acronym;
}
console.log(makeAbbr('I love you'));
console.log(makeAbbr('I     love     you'));
console.log(makeAbbr('   I    love    you   '));
console.log(makeAbbr('   \tI ...  ! love \n\r   .you   '));
console.log(makeAbbr('   \tI ...  ! Löve \n\r   .ÿou   '));

about 4 years ago · Juan Pablo Isaza Relatório

0

here is my implementation of your function: Split the sentence into an array, get the first letter of each word and join them into one string.

const makeAbbr = string => string.split(' ').map(word => word[0]).join('');

console.log(makeAbbr('stack overflow'));
console.log(makeAbbr('i love you'));

`

about 4 years ago · Juan Pablo Isaza Relatório

0

Since you wanted something using your approach, try this (code is commented)

function makeAbbr(words) {
  let abbrev = "";
  for (let i = 0; i < words.length - 1; i++) { // Loop through every character except the last one
    if (i == 0 && words[i] != " ") { // Add the first character
      abbrev += words[i];
    } else if (words[i] == " " && words[i + 1] != " ") { // If current character is space and next character isn't
      abbrev += words[i + 1];
    }
  }
  return abbrev.toLowerCase();
}
const words = 'a bc def';
let result = makeAbbr(words);
console.log(result)

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