|
1 | 1 | /**
|
2 | 2 | * @function upper
|
3 | 3 | * @description Will convert the entire string to uppercase letters.
|
4 | | - * @param {String} url - The input URL string |
| 4 | + * @param {String} str - The input string |
5 | 5 | * @return {String} Uppercase string
|
6 | 6 | * @example upper("hello") => HELLO
|
7 | 7 | * @example upper("He_llo") => HE_LLO
|
8 | 8 | */
|
9 | | - |
10 | 9 | const upper = (str) => {
|
11 | 10 | if (typeof str !== 'string') {
|
12 | | - throw new TypeError('Invalid Input Type') |
| 11 | + throw new TypeError('Argument should be string') |
13 | 12 | }
|
14 | 13 |
|
15 | | - let upperString = '' |
| 14 | + return str.replace( |
| 15 | + /[a-z]/g, |
| 16 | + (_, indexOfLowerChar) => { |
| 17 | + const asciiCode = str.charCodeAt(indexOfLowerChar) |
16 | 18 |
|
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) |
21 | 20 | }
|
22 | | - upperString += String.fromCharCode(asciiCode) |
23 | | - } |
24 | | - |
25 | | - return upperString |
| 21 | + ) |
26 | 22 | }
|
27 | 23 |
|
28 | 24 | export { upper }
|
0 commit comments