mirror of
https://github.com/TheAlgorithms/JavaScript.git
synced 2025-07-05 00:01:37 +08:00
merge: Added replace method to Upper
(#916)
* feat: added replace method * resolve: fix algo
This commit is contained in:
@ -1,28 +1,24 @@
|
|||||||
/**
|
/**
|
||||||
* @function upper
|
* @function upper
|
||||||
* @description Will convert the entire string to uppercase letters.
|
* @description Will convert the entire string to uppercase letters.
|
||||||
* @param {String} url - The input URL string
|
* @param {String} str - The input string
|
||||||
* @return {String} Uppercase string
|
* @return {String} Uppercase string
|
||||||
* @example upper("hello") => HELLO
|
* @example upper("hello") => HELLO
|
||||||
* @example upper("He_llo") => HE_LLO
|
* @example upper("He_llo") => HE_LLO
|
||||||
*/
|
*/
|
||||||
|
|
||||||
const upper = (str) => {
|
const upper = (str) => {
|
||||||
if (typeof str !== 'string') {
|
if (typeof str !== 'string') {
|
||||||
throw new TypeError('Invalid Input Type')
|
throw new TypeError('Argument should be string')
|
||||||
}
|
}
|
||||||
|
|
||||||
let upperString = ''
|
return str.replace(
|
||||||
|
/[a-z]/g,
|
||||||
|
(_, indexOfLowerChar) => {
|
||||||
|
const asciiCode = str.charCodeAt(indexOfLowerChar)
|
||||||
|
|
||||||
for (const char of str) {
|
return String.fromCharCode(asciiCode - 32)
|
||||||
let asciiCode = char.charCodeAt(0)
|
|
||||||
if (asciiCode >= 97 && asciiCode <= 122) {
|
|
||||||
asciiCode -= 32
|
|
||||||
}
|
}
|
||||||
upperString += String.fromCharCode(asciiCode)
|
)
|
||||||
}
|
|
||||||
|
|
||||||
return upperString
|
|
||||||
}
|
}
|
||||||
|
|
||||||
export { upper }
|
export { upper }
|
||||||
|
Reference in New Issue
Block a user