From 3c0cea90d573825242ffbf0d3d8db25a42ad1317 Mon Sep 17 00:00:00 2001 From: fahimfaisaal Date: Fri, 4 Mar 2022 21:37:40 +0600 Subject: [PATCH 1/2] feat: added replace method --- String/Upper.js | 20 ++++++-------------- 1 file changed, 6 insertions(+), 14 deletions(-) diff --git a/String/Upper.js b/String/Upper.js index d9aaceae3a..655d2a9bfb 100644 --- a/String/Upper.js +++ b/String/Upper.js @@ -1,28 +1,20 @@ /** * @function upper * @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 * @example upper("hello") => HELLO * @example upper("He_llo") => HE_LLO */ - const upper = (str) => { if (typeof str !== 'string') { - throw new TypeError('Invalid Input Type') - } - - let upperString = '' - - for (const char of str) { - let asciiCode = char.charCodeAt(0) - if (asciiCode >= 97 && asciiCode <= 122) { - asciiCode -= 32 - } - upperString += String.fromCharCode(asciiCode) + throw new TypeError('Argument should be string') } - return upperString + return str.replace( + /[a-z]/g, + (lowerLetter) => lowerLetter.toUpperCase() + ) } export { upper } From 52c02c68b1b81cff2cde3da835d3445f381d559a Mon Sep 17 00:00:00 2001 From: fahimfaisaal Date: Fri, 4 Mar 2022 21:57:17 +0600 Subject: [PATCH 2/2] resolve: fix algo --- String/Upper.js | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/String/Upper.js b/String/Upper.js index 655d2a9bfb..3ec1611e76 100644 --- a/String/Upper.js +++ b/String/Upper.js @@ -13,7 +13,11 @@ const upper = (str) => { return str.replace( /[a-z]/g, - (lowerLetter) => lowerLetter.toUpperCase() + (_, indexOfLowerChar) => { + const asciiCode = str.charCodeAt(indexOfLowerChar) + + return String.fromCharCode(asciiCode - 32) + } ) }