mirror of
https://github.com/TheAlgorithms/JavaScript.git
synced 2025-07-05 16:26:47 +08:00
merge: Add lower (#863)
This commit is contained in:
28
String/Lower.js
Normal file
28
String/Lower.js
Normal file
@ -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 }
|
9
String/test/Lower.test.js
Normal file
9
String/test/Lower.test.js
Normal file
@ -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')
|
||||
})
|
||||
})
|
Reference in New Issue
Block a user