Skip to content

Commit ae5076b

Browse files
authored
Merge pull request #11 from imravichhetri/toUpperCase
toUpperCase implemented
2 parents 70fcfc0 + 5bc9df9 commit ae5076b

File tree

1 file changed

+24
-0
lines changed

1 file changed

+24
-0
lines changed

implementations/toUpperCase.js

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
/*
2+
The toUpperCase() method returns the value of the string converted to uppercase.
3+
This method does not affect the value of the string itself since JavaScript strings are immutable.
4+
5+
6+
MDN Link: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/toUpperCase
7+
8+
Characters from A-Z have ASCII code from 65 - 90.
9+
And characters from a-z have ASCII code from 97-122.
10+
We're checking this condition to implement this function.
11+
*/
12+
String.prototype.toUpperCase = function myToUpperCase() {
13+
let upperCaseString = '';
14+
for (let i = 0; i < this.length; i += 1) {
15+
const character = this[i];
16+
const charCode = character.charCodeAt();
17+
if (charCode >= 97 && charCode <= 122) {
18+
upperCaseString += String.fromCharCode(charCode - 32);
19+
} else {
20+
upperCaseString += character;
21+
}
22+
}
23+
return upperCaseString;
24+
};

0 commit comments

Comments
 (0)