forked from TonnyL/Windary
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathToLowerCase.js
55 lines (50 loc) · 1.07 KB
/
ToLowerCase.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
/**
* Implement function ToLowerCase() that has a string parameter str, and returns the same string in lowercase.
* <p>
* Example 1:
* <p>
* Input: "Hello"
* Output: "hello"
* Example 2:
* <p>
* Input: "here"
* Output: "here"
* Example 3:
* <p>
* Input: "LOVELY"
* Output: "lovely"
* <p>
* Accepted.
*/
/**
* @param {string} str
* @return {string}
*/
let toLowerCase = function (str) {
// return str.toLowerCase()
let builder = "";
for (let index = 0; index < str.length; index++) {
let asciiValue = str.charCodeAt(index);
if (asciiValue >= 65 && asciiValue <= 90) {
builder += String.fromCharCode(asciiValue + 32);
} else {
builder += str.charAt(index)
}
}
return builder;
};
if (toLowerCase("Hello") === "hello") {
console.log("pass");
} else {
console.error("failed");
}
if (toLowerCase("here") === "here") {
console.log("pass");
} else {
console.error("failed");
}
if (toLowerCase("LOVELY") === "lovely") {
console.log("pass");
} else {
console.error("failed");
}