Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

merge: Upgrade Lower function #894

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
raklaptudirm merged 5 commits into TheAlgorithms:master from fahimfaisaal:upgrade-lower
Feb 19, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 7 additions & 12 deletions String/Lower.js
View file Open in desktop
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
/**
* @function lower
* @description Will convert the entire string to lowercase letters.
* @param {String} url - The input URL string
* @return {String} Lowercase string
* @param {String} str - The input string
* @returns {String} Lowercase string
* @example lower("HELLO") => hello
* @example lower("He_llo") => he_llo
*/
Expand All @@ -12,17 +12,12 @@ const lower = (str) => {
throw new TypeError('Invalid Input Type')
}

let lowerString = ''
return str
.replace(/[A-Z]/g, (_, indexOfUpperChar) => {
const asciiCode = str.charCodeAt(indexOfUpperChar)

for (const char of str) {
let asciiCode = char.charCodeAt(0)
if (asciiCode >= 65 && asciiCode <= 90) {
asciiCode += 32
}
lowerString += String.fromCharCode(asciiCode)
}

return lowerString
return String.fromCharCode(asciiCode + 32)
})
}

export { lower }
18 changes: 14 additions & 4 deletions String/test/Lower.test.js
View file Open in desktop
Original file line number Diff line number Diff line change
@@ -1,9 +1,19 @@
import { lower } from '../Lower'

describe('Lower', () => {
it('return uppercase strings', () => {
expect(lower('hello')).toBe('hello')
describe('Testing the Lower function', () => {
it('Test 1: Check by invalid type', () => {
expect(() => lower(345)).toThrowError()
expect(() => lower(true)).toThrowError()
expect(() => lower(null)).toThrowError()
})

it('Test 2: Check by uppercase string', () => {
expect(lower('WORLD')).toBe('world')
expect(lower('hello_WORLD')).toBe('hello_world')
expect(lower('Hello_WORLD')).toBe('hello_world')
})

it('Test 3: Check by lowercase string', () => {
expect(lower('hello')).toBe('hello')
expect(lower('hello_world')).toBe('hello_world')
})
})

AltStyle によって変換されたページ (->オリジナル) /