How do I write a function in JS to return abbreviation of the words?
Asked Answered
F

5

7

For example:

makeAbbr('central processing unit') === 'CPU'

I could not find my mistake. I appreciate your help.

function makeAbbr(words) {
  let abbreviation = words[0];
  for (let i = 1; i < words.length; i++) {
    if (words[i] === '') {
      abbreviation += words[i + 1];
    }
  }
  return abbreviation.toUpperCase();
}

console.log(makeAbbr('central processing unit'));
Flintshire answered 31/5, 2022 at 10:44 Comment(0)
K
5

You just need to change the words[i] === '' into words[i] === ' '. '' is an empty string.

Another option is splitting the passed string.

function makeAbbr(str) {
   // words is [ "central", "processing", "unit" ]
   let words = str.split(/\s+/);
   let abbreviation = '';
   for (let i = 0; i < words.length; i++) {
     abbreviation += words[i][0];
   }
   return abbreviation.toUpperCase();
}
Klara answered 31/5, 2022 at 10:49 Comment(0)
C
5

This will generally work on abbreviation for words that use the first character of each words separated by a space.

function makeAbbr(words) {
  // result abbreviation
  let abbreviation = '';
  // store each word into an array using split by space
  let wordArray = words.split(' ');
  // iterate through the word array
  for (let i = 0; i < wordArray.length; i++) {
    // take the first character in each word into the result abbreviation
    abbreviation += wordArray[i][0];
  }
  // return the abbreviation with all of them being upper case
  return abbreviation.toUpperCase();
}
// test case
console.log(makeAbbr('central processing unit'));
Corrigendum answered 31/5, 2022 at 10:51 Comment(0)
L
2

A one liner solution:

function makeAbbr(words) {
  return words.split(' ').map(word => word[0].toUpperCase()).join("");
}

console.log(makeAbbr('central processing unit'));

We are converting the string sentence to an array of words separated by a space (.split(" ")), then mapping or transforming every word in the array to its first letter, as well as capitalizing it: .map(word => word[0].toUpperCase()), then joining the array elements into a string with a "nothing" "" as a separator:

"central processing unit" -> ["central", "processing", "unit"] -> ["C", "P", "U"] -> "CPU"

Lacroix answered 31/5, 2022 at 11:14 Comment(0)
R
0
const sample = ('central processing unit');

const makeAbrr = (word: string) => {
        return word.split(' ').map((letter) => letter[0].toUpperCase()).join('');
}

console.log(makeAbrr(sample));
Raspings answered 31/5, 2022 at 11:13 Comment(1)
word: string is a TypeScript thing, not JS.Cerement
O
0

Regex version

function makeAbbr(text) {
  if (typeof text != 'string' || !text) {
    return '';
  }
  const acronym = text
    .match(/[\p{Alpha}\p{Nd}]+/gu)
    .reduce((previous, next) => previous + ((+next === 0 || parseInt(next)) ? parseInt(next): next[0] || ''), '')
    .toUpperCase();
  return acronym;
}
console.log(makeAbbr('central processing unit'));

Reference

Offstage answered 31/5, 2022 at 11:23 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.