alternate case of each character
in a string
[ map | modulo operator | split | join | toLowerCase | toUpperCase ]

Alternate the case of each character in a string

Write a function that takes a string and returns a new string that alternates the case of each character.

The even-indexed characters are converted to upper case while the odd-indexed characters are converted to lower case.

Non-alphanumeric characters, such as spaces or punctuation, are treated as regular characters and will remain unchanged, and will appear in the same position in the output string.


Example ...

Enter a string ...

"hello world" original string

"HeLlO WoRlD" returns new string with alternating case

The new string alternates the case of each character between upper and lower 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


Arrays are used to store multiple values in a single variable.

Each value is called an element, and each element has a numeric position in the array, known as its index.

Arrays are zero-indexed, meaning the first element is at index 0, the second at index 1, and so on.

Arrays can contain any data type, including numbers, strings, and objects.

const arr1 = [2, 4, 6]; array

arr1[0]; element at index 0 → 2

arr1[1]; element at index 1 → 4

arr1[2]; element at index 2 → 6

arr1[3]; element 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


Alternate the case of each character in a string using ↴

map() method → creates a new array from calling a function for every array element.

split() method → splits a string into an array of substrings.

join() method → returns an array as a string.

ternary operator → frequently used as an alternative to an if...else statement.

modulo operator % (remainder operator) → returns the remainder left over when one operand is divided by a second operand.

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

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


map() method creates a new array populated with the results of calling a provided function on every element in the calling array. The original array is unchanged.

const arr2 = [5, 10, 15, 20];

arr2.map((x) => x + 10); returns ↴

[15, 20, 25, 30] → 10 added to each element


split() method splits a string into an array of substrings based on a specified separator. The original string is unchanged.

("") separator → string is split between each character.

(" ") separator → string is split between words.

const str3 = "Hello"; → string

str3.split(""); returns ↴

["H", "e", "l", "l", "o"] → array

const str4 = "hello world"; → string

str4.split(" "); returns ↴

["hello", "world"] → array


join() method joins all elements of an array into a string based on a specified separator. The original array is unchanged.

("") separator → string is joined with no spaces between each character.

(" ") separator → string is joined with no spaces between each word.

const arr3 = ["H", "e", "l", "l", "o"]; array

arr3.join(""); returns ↴

"Hello" → string

const arr4 = ["Hello", "World"]; array

arr4.join(" "); returns ↴

"Hello World" → string


Modulo operator % (remainder operator) returns the remainder left over when one number is divided by a second number.

10 % 2 remainder 0

10 % 3 remainder 1

10 % 4 remainder 2

10 % 5 remainder 0

The modulo operator can be used to check whether a number is odd or even.

If a number is divisible by 2 (with no remainder) then it must be an even number.

If a number is not divisible by 2 (with a remainder) then it must be an odd number.

4 % 2 === 0; statement is true, remainder is zero, so 4 is an even number.

5 % 2 === 0; statement is false, remainder is not zero, so 5 is an odd number.

function checkNumber(num) {

if (num % 2 === 0) { if num divisible by 2

console.log(num + " is even"); if true

} else {

console.log(num + " is odd"); if false

}

}

checkNumber(2); 2 is even

checkNumber(3); 3 is odd

checkNumber(4); 4 is even

checkNumber(5); 5 is odd


Ternary Operator

Ternary Operator is frequently used as an alternative to if...else statements.

if (condition) {

expressionIfTrue;

} else {

expressionIfFalse;

}

Ternary Operator takes three operands ↴

condition evaluates to true or false.

? question mark → expression to execute if the condition is truthy

: colon → expression to execute if the condition is falsy

condition ? expressionIfTrue : expressionIfFalse

let score = 75;

let result = score >= 55

? "You passed"

: "You failed";

console.log(result); returns ↴

You passed → first expression executed


toLowerCase() method returns a new string with all letters converted to lower case. The original string is unchanged.

const str5 = "hELlo wORLd";

str5.toLowerCase(); returns ↴

"hello world" → lower case


toUpperCase() method returns a new string with all letters converted to upper case. The original string is unchanged.

const str6 = "hELlo wORLd";

str6.toUpperCase(); returns ↴

"HELLO WORLD" → upper case


Initialize a variable to hold the string to alternate case.

const string1 = "hello world"; → user input


Define a function alternateCase to alternate the case of a string.

function alternateCase(str) {}

The function takes a string as input str and returns a new string with alternating case. The original string remains unchanged.

Split str into an array of characters.

return str.split("")

map() method creates a new array populated with the results of calling a provided function on every element in the calling array.

map(calbackFn)

The map method iterates over each character, providing both the character char and its index

.map((char, index) => iterate over each character with its index

index % 2 == 0 ? char.toUpperCase() : char.toLowerCase()) callback function

The conditional expression index % 2 == 0 checks if the index is even.

If true, the ternary operator converts the character to upper case.

If false, the ternary operator converts the character to lower case.

Join the array back into a single string.

.join("");


Call the function with ↴

alternateCase(string1);


Alternate case of each character in a string.

const string1 = "hello world";

function alternateCase(str) {

return str

.split("")

.map((char, index) =>

index % 2 == 0 ? char.toUpperCase() : char.toLowerCase()

)

.join("");

}

call function

alternateCase(string1); returns ↴

"HeLlO WoRlD"

Alternate case of each character in a string