Skip to content

Commit 4c27e15

Browse files
merge: Add lower (TheAlgorithms#863)
1 parent 62b151e commit 4c27e15

File tree

2 files changed

+37
-0
lines changed

2 files changed

+37
-0
lines changed

String/Lower.js

+28
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
/**
2+
* @function lower
3+
* @description Will convert the entire string to lowercase letters.
4+
* @param {String} url - The input URL string
5+
* @return {String} Lowercase string
6+
* @example lower("HELLO") => hello
7+
* @example lower("He_llo") => he_llo
8+
*/
9+
10+
const lower = (str) => {
11+
if (typeof str !== 'string') {
12+
throw new TypeError('Invalid Input Type')
13+
}
14+
15+
let lowerString = ''
16+
17+
for (const char of str) {
18+
let asciiCode = char.charCodeAt(0)
19+
if (asciiCode >= 65 && asciiCode <= 90) {
20+
asciiCode += 32
21+
}
22+
lowerString += String.fromCharCode(asciiCode)
23+
}
24+
25+
return lowerString
26+
}
27+
28+
export { lower }

String/test/Lower.test.js

+9
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
import { lower } from '../Lower'
2+
3+
describe('Lower', () => {
4+
it('return uppercase strings', () => {
5+
expect(lower('hello')).toBe('hello')
6+
expect(lower('WORLD')).toBe('world')
7+
expect(lower('hello_WORLD')).toBe('hello_world')
8+
})
9+
})

0 commit comments

Comments
 (0)