Skip to content

Commit d1d5ca9

Browse files
authored
Merge pull request #7 from imravichhetri/toLowerCase
toLowerCase implemented
2 parents ae5076b + 58bc1aa commit d1d5ca9

File tree

1 file changed

+24
-0
lines changed

1 file changed

+24
-0
lines changed

implementations/toLowerCase.js

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
/*
2+
3+
The toLowerCase() method returns the value of the string converted to lower case.
4+
toLowerCase() does not affect the value of the string str itself.
5+
6+
MDN Link: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/toLowerCase
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.toLowerCase = function myToLowerCase() {
13+
let lowerCaseString = '';
14+
for (let i = 0; i < this.length; i += 1) {
15+
const character = this[i];
16+
const charCode = character.charCodeAt();
17+
if (charCode >= 65 && charCode <= 90) {
18+
lowerCaseString += String.fromCharCode(charCode + 32);
19+
} else {
20+
lowerCaseString += character;
21+
}
22+
}
23+
return lowerCaseString;
24+
};

0 commit comments

Comments
 (0)