Skip to content

Commit 35ba618

Browse files
authored
merge: Added replace method to Upper (#916)
* feat: added replace method * resolve: fix algo
1 parent 00f5936 commit 35ba618

File tree

1 file changed

+8
-12
lines changed

1 file changed

+8
-12
lines changed

String/Upper.js

+8-12
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,24 @@
11
/**
22
* @function upper
33
* @description Will convert the entire string to uppercase letters.
4-
* @param {String} url - The input URL string
4+
* @param {String} str - The input string
55
* @return {String} Uppercase string
66
* @example upper("hello") => HELLO
77
* @example upper("He_llo") => HE_LLO
88
*/
9-
109
const upper = (str) => {
1110
if (typeof str !== 'string') {
12-
throw new TypeError('Invalid Input Type')
11+
throw new TypeError('Argument should be string')
1312
}
1413

15-
let upperString = ''
14+
return str.replace(
15+
/[a-z]/g,
16+
(_, indexOfLowerChar) => {
17+
const asciiCode = str.charCodeAt(indexOfLowerChar)
1618

17-
for (const char of str) {
18-
let asciiCode = char.charCodeAt(0)
19-
if (asciiCode >= 97 && asciiCode <= 122) {
20-
asciiCode -= 32
19+
return String.fromCharCode(asciiCode - 32)
2120
}
22-
upperString += String.fromCharCode(asciiCode)
23-
}
24-
25-
return upperString
21+
)
2622
}
2723

2824
export { upper }

0 commit comments

Comments
 (0)