diff --git a/String/Lower.js b/String/Lower.js new file mode 100644 index 0000000000..3805889948 --- /dev/null +++ b/String/Lower.js @@ -0,0 +1,28 @@ +/** + * @function lower + * @description Will convert the entire string to lowercase letters. + * @param {String} url - The input URL string + * @return {String} Lowercase string + * @example lower("HELLO") => hello + * @example lower("He_llo") => he_llo + */ + +const lower = (str) => { + if (typeof str !== 'string') { + throw new TypeError('Invalid Input Type') + } + + let lowerString = '' + + for (const char of str) { + let asciiCode = char.charCodeAt(0) + if (asciiCode >= 65 && asciiCode <= 90) { + asciiCode += 32 + } + lowerString += String.fromCharCode(asciiCode) + } + + return lowerString +} + +export { lower } diff --git a/String/test/Lower.test.js b/String/test/Lower.test.js new file mode 100644 index 0000000000..0fbaaa5d62 --- /dev/null +++ b/String/test/Lower.test.js @@ -0,0 +1,9 @@ +import { lower } from '../Lower' + +describe('Lower', () => { + it('return uppercase strings', () => { + expect(lower('hello')).toBe('hello') + expect(lower('WORLD')).toBe('world') + expect(lower('hello_WORLD')).toBe('hello_world') + }) +})