
- 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
Creating permutations by changing case in JavaScript
Problem
We are required to write a JavaScript function that takes in a string of characters, str, as the first and the only argument.
Our function can transform every letter individually to be lowercase or uppercase to create another string. And we should return a list of all possible strings we could create.
For example, if the input to the function is
Input
const str = 'k1l2';
Output
const output = ["k1l2","k1L2","K1l2","K1L2"];
Example
Following is the code −
const str = 'k1l2'; const changeCase = function (S = '') { const res = [] const helper = (ind = 0, current = '') => { if (ind >= S.length) { res.push(current) return } if (/[a-zA-Z]/.test(S[ind])) { helper(ind + 1, current + S[ind].toLowerCase()) helper(ind + 1, current + S[ind].toUpperCase()) } else { helper(ind + 1, current + S[ind]) } } helper() return res }; console.log(changeCase(str));
Output
[ 'k1l2', 'k1L2', 'K1l2', 'K1L2' ]
- Related Articles
- Creating all possible unique permutations of a string in JavaScript
- Changing the case of a string using JavaScript
- Creating permutations to reach a target number, but reuse the provided numbers JavaScript
- Calculating Josephus Permutations efficiently in JavaScript
- Create palindrome by changing each character to neighboring character in JavaScript
- Changing color randomly in JavaScript
- Number prime test in JavaScript by creating a custom function?
- Creating an empty case-sensitive HybridDictionary Class in C#
- Creating an empty HybridDictionary with specified case sensitivity in C#
- Generating all possible permutations of array in JavaScript
- Creating a Stack in Javascript
- Creating a Queue in Javascript
- Creating a Graph in Javascript
- Creating a Case-Sensitive HybridDictionary with specified initial size in C#
- Creating Arrays using Javascript

Advertisements