
- 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
Alternate casing a string in JavaScript
We are required to write a JavaScript function that takes in a string and constructs a new string with all the uppercase characters from the original string converted to lowercase and all the lowercase characters converted to uppercase from the original string.
For example: If the string is −
const str = 'The Case OF tHis StrinG Will Be FLiPped';
Output
Then the output should be −
const output = 'tHE cASE of ThIS sTRINg wILL bE flIpPED';
Example
The code for this will be −
const str = 'The Case OF tHis StrinG Will Be FLiPped'; const isUpperCase = char => char.charCodeAt(0) >= 65 && char.charCodeAt(0)<= 90; const isLowerCase = char => char.charCodeAt(0) >= 97 && char.charCodeAt(0) <= 122; const flipCase = str => { let newStr = ''; const margin = 32; for(let i = 0; i < str.length; i++){ const curr = str[i]; if(isLowerCase(curr)){ newStr += String.fromCharCode(curr.charCodeAt(0) - margin); }else if(isUpperCase(curr)){ newStr += String.fromCharCode(curr.charCodeAt(0) + margin); }else{ newStr += curr; }; }; return newStr; }; console.log(flipCase(str));
Output
The output in the console −
tHE cASE of ThIS sTRINg wILL bE flipped
- Related Articles
- Alternate Lower Upper String Sort in C++
- Alternate vowel and consonant string in C++
- Alternate vowel and consonant string in C/C++?
- Alternate addition multiplication in an array - JavaScript
- Difference of digits at alternate indices in JavaScript
- JavaScript Program For Reversing Alternate K Nodes In A Singly Linked List
- JavaScript Program To Delete Alternate Nodes Of A Linked List
- Program for converting Alternate characters of a string to Upper Case.\n
- Number of flips to make binary string alternate - Set 1 in C++
- Interchanging a string to a binary string in JavaScript
- Uncamelising a string in JavaScript
- Truncating a String in JavaScript
- Check if it is possible to rearrange a binary string with alternate 0s and 1s in Python
- Keeping only alphanumerals in a JavaScript string in JavaScript
- Alternate Key in RDBMS

Advertisements