
- 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
Swapping string case using a binary number in JavaScript
Problem
We are required to write a JavaScript function that takes in a string str and a number n. Our function should change the given string str using n.
Each bit in n will specify whether or not to swap the case for each alphabetic character in s −
If the bit is 1, swap the case; if its 0, leave it as is. When we are finished with the last bit of n, start again with the first bit.
And finally, we should return the new string thus formed.
Example
Following is the code −
const str = 'hey there'; const num = 21; const swapCase = (str = '', num = 1) => { const alphaLength = str .split('') .reduce((acc, val) => val.toLowerCase() !== val.toUpperCase() ? ++acc : acc, 0); let binary = num.toString(2); while(binary.length < alphaLength){ binary += binary; }; let res = ''; for(let i = 0; i < str.length; i++){ const el = str[i]; if(el.toUpperCase() !== el.toLowerCase() && +binary[i] === 1){ if(el.toLowerCase() === el){ res += el.toUpperCase(); }else{ res += el.toLowerCase(); } }else{ res += el; }; }; return res; }; console.log(swapCase(str, num));
Output
Following is the console output −
HeY TheRe
- Related Articles
- Swapping adjacent words of a String in JavaScript
- Swapping adjacent binary bits of a decimal to yield another decimal using JavaScript
- Changing the case of a string using JavaScript
- Finding minimum flips in a binary string using JavaScript
- How to convert a string into upper case using JavaScript?
- Interchanging a string to a binary string in JavaScript
- How to convert a string into the lower case using JavaScript?
- Swapping Characters of a String in Java
- Swapping Characters of a String in C#
- Converting string to a binary string - JavaScript
- Convert mixed case string to lower case in JavaScript
- Extract a number from a string using JavaScript
- Casting a string to snake case - JavaScript
- Find number of spaces in a string using JavaScript
- String to binary in JavaScript

Advertisements