

- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Returning acronym based on a string in JavaScript
We are required to write a JavaScript function that takes in a string of characters as the only argument.
The function should build and return the acronym based on the string phrase provided as input.
While constructing the acronym the function should take only those words into consideration that starts with an uppercase letter.
For example −
If the input string is −
const str = 'Polar Satellite Launch Vehicle';
Then the output should be −
const output = 'PSLV';
Example
Following is the code −
const str = 'Polar Satellite Launch Vehicle'; const buildAcronym = (str = '') => { const strArr = str.split(' '); let res = ''; strArr.forEach(el => { const [char] = el; if(char === char.toUpperCase() && char !== char.toLowerCase()){ res += char; }; }); return res; }; console.log(buildAcronym(str)); console.log(buildAcronym('Bachelor of Science'));
Output
Following is the console output −
PSLV BS
- Related Questions & Answers
- Change string based on a condition - JavaScript
- Encrypting a string based on an algorithm in JavaScript
- Encrypting a string based on an algorithm using JavaScript
- Shuffling string based on an array in JavaScript
- Encoding string based on character frequency in JavaScript
- Returning lengthy words from a string using JavaScript
- Shifting string letters based on an array in JavaScript
- Constructing a string based on character matrix and number array in JavaScript
- Acronym in Python
- Forming and matching strings of an array based on a random string in JavaScript
- Get part of a string based on a character in MySQL?
- JavaScript Separate objects based on properties
- Return a splitted array of the string based on all the specified separators - JavaScript
- Returning values from a constructor in JavaScript?
- Reorder array based on condition in JavaScript?
Advertisements