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

263
Views
How to make abbreviations using a function?

Here is my code, but it does not work.

function makeAbbr(words) {
  var text = '';
  
  words.split(' ');
  
  for (i = 0; i < words.length; i++) {
    text += words[i].substr(0, 1)
  }
  
  return text
}

console.log(makeAbbr('java script'))

about 4 years ago · Juan Pablo Isaza
2 answers
Answer question

0

The following should work:

function makeAbbr(someStr) {
  const words = someStr.split(" ");
  let abbr = "";
  for (const word of words) {
    abbr += word.substring(0,1);
  }
  return abbr;
}
makeAbbr("I love JavaScript!"); // IlJ

.split(...) does not mutate the string (and convert it to an array), but returns the array. Therefore, you have to assign the result to a variable.

about 4 years ago · Juan Pablo Isaza Report

0

/**

 * @param {string} words

 *

 * @returns {string}

 */

function makeAbbr(words) {

  let wor = words.split(" ");

  let abbreviation = "";

  for (const word of wor) {

    

  abbreviation += word[0].substring();;

  }

  return abbreviation.toUpperCase();

}

about 4 years ago · Ihor Moskovenko 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!