Skip to content

Commit 00baf23

Browse files
add UpperCaseConversion method
1 parent f6745d2 commit 00baf23

File tree

1 file changed

+35
-0
lines changed

1 file changed

+35
-0
lines changed

Conversions/UpperCaseConversion.js

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
/*
2+
Explanation :- a user gives a String (it can be incomplete lower or
3+
partial lower) and then the program would convert it into a
4+
complete(all characters in upper case) upper case string. The
5+
logic we have used in the following program is: All the lower case
6+
characters (a-z) has ASCII value ranging from 97 to 122 and their
7+
corresponding upper case characters (A-Z) have ASCII values 32
8+
lesser than them. For example ‘a‘ has an ASCII value of 97
9+
and ‘A‘ has an ASCII value of 65 (97 - 32). The same applies to other
10+
characters.
11+
*/
12+
13+
/**
14+
* UpperCaseConversion takes any case-style string and converts it to the upper case-style string.
15+
* @param {String} inputString any case style string
16+
* @returns {String} upper case string
17+
*/
18+
const UpperCaseConversion = (inputString) => {
19+
// Take a string and split it into characters.
20+
const newString = inputString.split('').map(char => {
21+
// Get a character code by the use charCodeAt method.
22+
const presentCharCode = char.charCodeAt()
23+
// If the character code lies between 97 to 122 it means they are in the lower case so convert it.
24+
if (presentCharCode >= 97 && presentCharCode <= 122) {
25+
// Convert the case by use of the above explanation.
26+
return String.fromCharCode(presentCharCode - 32)
27+
}
28+
// Else return the characters without any modification.
29+
return char
30+
})
31+
// After modification, with the help of the join method, join all the characters and return them.
32+
return newString.join('')
33+
}
34+
35+
module.exports = UpperCaseConversion

0 commit comments

Comments
 (0)