inverse case of each character
in a string
[ for-loop | charCodeAt | fromCharCode | toLowerCase | toUpperCase ]

Inverse the case of each character in a string

Write a function that takes a string and returns a new string that inverts the case of each character, converting upper case letters to lower case and lower case letters to upper case.


Example ...

Enter a string ...

"aaBBcc DDeeFF" original string

"AAbbCC ddEEff" returns new string with the case of each character inverted.

Upper case letters are converted to lower case, and lower case letters are converted to upper case.

The original string is unchanged.

Strings are a sequence of zero or more characters written inside quotes used to represent text.

Strings may consist of letters, numbers, symbols, words, or sentences.

Strings are immutable, they cannot be changed.

Each character in a string has an index.

The first character will be index 0 the second character will be index 1 and so on.

There are two ways to access an individual character in a string.

charAt() method

const str1 = "abc"; string

str1.charAt(0); character at index 0 → "a"

str1.charAt(1); character at index 1 → "b"

str1.charAt(2); character at index 2 → "c"

str1.charAt(3); character at index 3 → "" index not found

Alternatively use at() or slice() methods

bracket notation []

const str2 = "abc"; string

str2[0]; character at index 0 → "a"

str2[1]; character at index 1 → "b"

str2[2]; character at index 2 → "c"

str2[3]; character at index 3 → undefined index not found


Numbers are used to represent both integer and floating-point values.

Numbers are most commonly expressed in literal forms like 255 or 3.14159 ↴

let num1 = 5; → number

let num2 = 2.5; → number

let num3 = num1 + num2;

console.log(num3); returns ↴

7.5 → number


Unicode

Unicode is an international character encoding standard that provides a unique number for every character across languages and scripts, making almost all characters accessible across platforms, programs, and devices.

UTF-16 code unit Unicode Transformation Format (UTF-16) is a character encoding system that uses 16-bit code units to represent Unicode code points.

Unicode values for the ASCII character set

Upper case characters from A to Z

Lower case characters from a to z

A65 a97

B66 b98

C67 c99

D68 d100

E69 e101

F70 f102

G71 g103

H72 h104

I73 i105

J74 j106

K75 k107

L76 l108

M77 m109

N78 n110

O79 o111

P80 p112

Q81 q113

R82 r114

S83 s115

T84 t116

U85 u117

V86 v118

W87 w119

X88 x120

Y89 y121

Z90 z122

Character a has unicode value 97

Character A has unicode value 65

Difference between A and a 97-65 = 32

Upper case characters can be derived from lower case characters, and vice versa.

Unicode value of character a97

Unicode value of character A97-32 = 65

Unicode value of character A65

Unicode value of character a65+32 = 97


Inverse case of each character in a string using ↴

for loop → executes a block of code a number of times.

charCodeAt() method → values returns an integer between 0 and 65535 representing the UTF-16 code unit at the given index.

String.fromCharCode() static method → returns a string created from the specified sequence of UTF-16 code units.

toLowerCase() method → returns the value of the string converted to lower case.

toUpperCase() method → returns the value of the string converted to upper case.


for loop repeatedly executes a block of code until a specified condition evaluates to false.

The loop runs a block of code a set number of times, defined by an initialization, a condition, and an increment.

for (let x = 0; x < 4; x++) {

console.log(x);

}

Loop variable x is initialized to 0

Condition x < 4 is checked before each iteration.

The loop will continue to run as long as x is less than 4

The loop repeatedly executes a block of code 4 times, from 0 to 3

For each iteration of the loop, the current value of x is printed to the console.

After each iteration, x is incremented by 1 x++

When x reaches 4 the condition evaluates to false, terminating the loop.

0

1

2

3 → printed to console


charCodeAt method returns an integer between 0 and 65535 representing the UTF-16 code unit at the given index.

const str3 = "ABCDEF";

str3.charCodeAt(2); returns ↴

67 → unicode value for character at index 2


String.fromCharCode static method returns a string created from the specified sequence of UTF-16 code units.

const num4 = 67;

String.fromCharCode(num4); returns ↴

"C" → character with unicode value of 67


toLowerCase() method converts all letters to lower case. The original string is unchanged.

const str4 = "hELlo wORLd";

str4.toLowerCase(); returns ↴

"hello world" → lower case


toUpperCase() method converts all letters to upper case. The original string is unchanged.

const str5 = "hELlo wORLd";

str5.toUpperCase(); returns ↴

"HELLO WORLD" → upper case


Initialize a variable to hold the string to inverse case.

const string1 = "aaBBccDDeeFFggHH"; → user input


Define a function inverseCase to inverse the case of each character in a string.

function inverseCase(str) {}

The function takes a string as input str and returns a new string with the case of all characters inverted. The original string remains unchanged.

The function uses two helper functions isUpperCase and isLowerCase to determine if a character is upper case or lower case.

The main function inverseCase processes the input string str and constructs a new string with inverted cases.

Helper function to check if a character is upper case → returns boolean

const isUpperCase = (char) =>

char.charCodeAt(0) >= 65 && char.charCodeAt(0) <= 90 isUpperCase

Helper function to check if a character is lower case → returns boolean

const isLowerCase = (char) =>

char.charCodeAt(0) >= 97 && char.charCodeAt(0) <= 122 isLowerCase

Main function to invert the case of characters in a string.

function inverseCase(str) {}

Initialize an empty string to hold the result.

let newStr = "" newStr

The difference in character codes between upper and lower case.

const margin = 32 margin

Loop through each character in str

for (let x = 0; x < str.length; x++) {}

Get the current character str[x]

const curr = str[x] curr

Check if the character is lower case.

if (isLowerCase(curr)) {}

If true, convert to upper case by subtracting margin

newStr += String.fromCharCode(curr.charCodeAt(0) - margin)

else, check if the character is upper case.

else if (isUpperCase(curr))

If true, convert to lower case by adding margin

newStr += String.fromCharCode(curr.charCodeAt(0) + margin)

else, if the character is neither, add it unchanged.

newStr += curr

Return the new string with inverted cases.

return newStr


Call the function with ↴

inverseCase(string1);


Inverse case of each character.

const string1 = "aaBBccDDeeFFggHH";

helper functions ↴

const isUpperCase = (char) => char.charCodeAt(0) >= 65 && char.charCodeAt(0) <= 90;

const isLowerCase = (char) => char.charCodeAt(0) >= 97 && char.charCodeAt(0) <= 122;

function inverseCase(str) {

let newStr = "";

const margin = 32;

for (let x = 0; x < str.length; x++) {

const curr = str[x];

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;

}

call function

inverseCase(string1); returns ↴

"AAbbCCddEEffGGhh"

Inverse case of each character