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

Commit 9681688

Browse files
merge: Add CheckExceeding function (#907)
* feat: add checkExceeding function * test: add test cases of checkExceeding func * test: add more not increasing words
1 parent d246958 commit 9681688

File tree

2 files changed

+88
-0
lines changed

2 files changed

+88
-0
lines changed

‎String/CheckExceeding.js‎

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
/**
2+
* @function checkExceeding
3+
* @description - Exceeding words are words where the gap between two adjacent characters is increasing. The gap is the distance in ascii
4+
* @param {string} str
5+
* @returns {boolean}
6+
* @example - checkExceeding('delete') => true, ascii difference - [1, 7, 7, 15, 15] which is incremental
7+
* @example - checkExceeding('update') => false, ascii difference - [5, 12, 3, 19, 15] which is not incremental
8+
*/
9+
const checkExceeding = (str) => {
10+
if (typeof str !== 'string') {
11+
throw new TypeError('Argument is not a string')
12+
}
13+
14+
const upperChars = str
15+
.toUpperCase()
16+
.replace(/[^A-Z]/g, '') // remove all from str except A to Z alphabets
17+
18+
const adjacentDiffList = []
19+
20+
for (let i = 0; i < upperChars.length - 1; i++) {
21+
// destructuring current char & adjacent char by index, cause in javascript String is an object.
22+
const { [i]: char, [i + 1]: adjacentChar } = upperChars
23+
24+
if (char !== adjacentChar) {
25+
adjacentDiffList.push(
26+
Math.abs(char.charCodeAt() - adjacentChar.charCodeAt())
27+
)
28+
}
29+
}
30+
31+
for (let i = 0; i < adjacentDiffList.length - 1; i++) {
32+
const { [i]: charDiff, [i + 1]: secondCharDiff } = adjacentDiffList
33+
34+
if (charDiff > secondCharDiff) {
35+
return false
36+
}
37+
}
38+
39+
return true
40+
}
41+
42+
export { checkExceeding }

‎String/test/CheckExceeding.test.js‎

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
import { checkExceeding } from '../CheckExceeding'
2+
3+
describe('Testing CheckExceeding function', () => {
4+
it('Testing the invalid types', () => {
5+
expect(() => checkExceeding(Math.random())).toThrow('Argument is not a string')
6+
expect(() => checkExceeding(null)).toThrow('Argument is not a string')
7+
expect(() => checkExceeding(false)).toThrow('Argument is not a string')
8+
expect(() => checkExceeding(false)).toThrow('Argument is not a string')
9+
})
10+
11+
it('Testing with empty string', () => {
12+
expect(checkExceeding('')).toBe(true)
13+
})
14+
15+
it('Testing with linear alphabets', () => {
16+
expect(checkExceeding('a b c d e ')).toBe(true)
17+
expect(checkExceeding('f g h i j ')).toBe(true)
18+
expect(checkExceeding('k l m n o ')).toBe(true)
19+
expect(checkExceeding('p q r s t ')).toBe(true)
20+
expect(checkExceeding('u v w x y z')).toBe(true)
21+
})
22+
23+
it('Testing not exceeding words', () => {
24+
expect(checkExceeding('Hello')).toBe(false)
25+
expect(checkExceeding('world')).toBe(false)
26+
expect(checkExceeding('update')).toBe(false)
27+
expect(checkExceeding('university')).toBe(false)
28+
expect(checkExceeding('dog')).toBe(false)
29+
expect(checkExceeding('exceeding')).toBe(false)
30+
expect(checkExceeding('resolved')).toBe(false)
31+
expect(checkExceeding('future')).toBe(false)
32+
expect(checkExceeding('fixed')).toBe(false)
33+
expect(checkExceeding('codes')).toBe(false)
34+
expect(checkExceeding('facebook')).toBe(false)
35+
expect(checkExceeding('vscode')).toBe(false)
36+
})
37+
38+
it('Testing exceeding words', () => {
39+
expect(checkExceeding('bee')).toBe(true) // [ 3 ]
40+
expect(checkExceeding('can')).toBe(true) // [ 2, 13 ]
41+
expect(checkExceeding('good')).toBe(true) // [ 8, 11 ]
42+
expect(checkExceeding('bad')).toBe(true) // [ 1, 3 ]
43+
expect(checkExceeding('play')).toBe(true) // [ 4, 11, 24 ]
44+
expect(checkExceeding('delete')).toBe(true) // [1, 7, 7, 15, 15]
45+
})
46+
})

0 commit comments

Comments
(0)

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