-
Notifications
You must be signed in to change notification settings - Fork 22
Expand file tree
/
Copy pathtoUpperCase.js
More file actions
24 lines (21 loc) · 873 Bytes
/
toUpperCase.js
File metadata and controls
24 lines (21 loc) · 873 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
/*
The toUpperCase() method returns the value of the string converted to uppercase.
This method does not affect the value of the string itself since JavaScript strings are immutable.
MDN Link: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/toUpperCase
Characters from A-Z have ASCII code from 65 - 90.
And characters from a-z have ASCII code from 97-122.
We're checking this condition to implement this function.
*/
String.prototype.toUpperCase = function myToUpperCase() {
let upperCaseString = '';
for (let i = 0; i < this.length; i += 1) {
const character = this[i];
const charCode = character.charCodeAt();
if (charCode >= 97 && charCode <= 122) {
upperCaseString += String.fromCharCode(charCode - 32);
} else {
upperCaseString += character;
}
}
return upperCaseString;
};