- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Move string capital letters to front maintaining the relative order - JavaScript
We are required to write a JavaScript function that takes in a string with uppercase and lowercase letters. The function should return a string with all the uppercase letters moved to front of the string.
For example: If the input string is −
const str = 'heLLO woRlD';
Then the output should be −
const output = 'LLORDhe wol';
Example
Following is the code −
const str = 'heLLO woRlD'; const moveCapitalToFront = (str = '') => { let capitalIndex = 0; const newStrArr = []; for(let i = 0; i < str.length; i++){ if(str[i] !== str[i].toLowerCase()){ newStrArr.splice(capitalIndex, 0, str[i]); capitalIndex++; }else{ newStrArr.push(str[i]); }; }; return newStrArr.join(''); }; console.log(moveCapitalToFront(str));
Output
Following is the output in the console −
LLORDhe wol
- Related Articles
- How to move all capital letters to the beginning of the string in JavaScript?
- How to return a passed string with letters in alphabetical order in JavaScript?
- Python program to move spaces to front of string in single traversal
- Python code to move spaces to the front of string in a single traversal
- How to set the font to be displayed in small capital letters with JavaScript?
- How can we print all the capital letters of a given string in Java?
- How to set the order of the flexible item, relative to the rest with JavaScript?
- Reversing a string while maintaining the position of spaces in JavaScript
- Maintaining order in MySQL “IN” query?
- How to find capital letters with Regex in MySQL?
- How to input letters in capital letters in an edit box in Selenium with python?
- Move all zeros to the front of the linked list in C++
- Python - Inserting item in sorted list maintaining order
- How to get the maximum count of repeated letters in a string? JavaScript
- For the Hosts Locale how to convert a string to uppercase letters in JavaScript?

Advertisements