
- Javascript Basics Tutorial
- Javascript - Home
- Javascript - Overview
- Javascript - Syntax
- Javascript - Enabling
- Javascript - Placement
- Javascript - Variables
- Javascript - Operators
- Javascript - If...Else
- Javascript - Switch Case
- Javascript - While Loop
- Javascript - For Loop
- Javascript - For...in
- Javascript - Loop Control
- Javascript - Functions
- Javascript - Events
- Javascript - Cookies
- Javascript - Page Redirect
- Javascript - Dialog Boxes
- Javascript - Void Keyword
- Javascript - Page Printing
- JavaScript Objects
- Javascript - Objects
- Javascript - Number
- Javascript - Boolean
- Javascript - Strings
- Javascript - Arrays
- Javascript - Date
- Javascript - Math
- Javascript - RegExp
- Javascript - HTML DOM
- JavaScript Advanced
- Javascript - Error Handling
- Javascript - Validations
- Javascript - Animation
- Javascript - Multimedia
- Javascript - Debugging
- Javascript - Image Map
- Javascript - Browsers
- JavaScript Useful Resources
- Javascript - Questions And Answers
- Javascript - Quick Guide
- Javascript - Functions
- Javascript - Resources
Removing n characters from a string in alphabetical order in JavaScript
Problem
We are required to write a JavaScript function that takes in a lowercase alphabet string and number num.
Our function should remove num characters from the array in alphabetical order. It means we should first remove ‘a’ if they exist then ‘b’ , ‘c’ and so on until we hit the desired num.
Example
Following is the code −
const str = 'abascus'; const num = 4; const removeAlphabetically = (str = '', num = '') => { const legend = "abcdefghijklmnopqrstuvwxyz"; for(let i = 0; i < legend.length; i+=1){ while(str.includes(legend[i]) && num > 0){ str = str.replace(legend[i], ""); num -= 1; }; }; return str; }; console.log(removeAlphabetically(str, num));
Output
Following is the console output −
sus
- Related Articles
- Removing first k characters from string in JavaScript
- Removing all non-alphabetic characters from a string in JavaScript
- Check if the characters of a given string are in alphabetical order in Python
- How to return a passed string with letters in alphabetical order in JavaScript?
- JavaScript - Remove first n characters from string
- Removing adjacent duplicates from a string in JavaScript
- Remove all non-alphabetical characters of a String in Java?
- Print common characters of two Strings in alphabetical order in C++
- Removing a specific substring from a string in JavaScript
- Removing punctuations from a string using JavaScript
- Removing comments from array of string in JavaScript
- Python code to print common characters of two Strings in alphabetical order
- Java code to print common characters of two Strings in alphabetical order
- How to get max alphabetical character from the string in Python?\n
- Removing all spaces from a string using JavaScript

Advertisements