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

518
Views
¿Cómo hacer abreviaturas/acrónimos en JavaScript?

nuevo en la codificación , estoy tratando de hacer una función que haga "abreviaturas/acrónimos" de palabras, por ejemplo, 'Te amo' -> 'ily'. Intenté reescribir el código de muchas maneras, pero console.log solo me muestra la primera letra de la primera palabra dada.

 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 answers
Answer question

0

Sin utilizar matrices. Pero realmente deberías aprender sobre ellos.

  1. Comience recortando los espacios en blanco iniciales y finales.
  2. Agregue el primer carácter a su acrónimo.
  3. Recorra el resto de la cadena y agregue el carácter actual al acrónimo si el carácter anterior era un espacio (y el carácter actual no lo es).

 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 '));


Y aquí está la versión para 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 Report

0

aquí está mi implementación de su función: divida la oración en una matriz, obtenga la primera letra de cada palabra y únalas en una cadena.

 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 Report

0

Como quería algo usando su enfoque, intente esto (el código está comentado)

 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 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!