From 96816887971611128598308522934ec13fc44ac1 Mon Sep 17 00:00:00 2001 From: Fahim Faisaal <57553028+fahimfaisaal@users.noreply.github.com> Date: Tue, 22 Mar 2022 15:07:42 +0600 Subject: [PATCH] merge: Add `CheckExceeding` function (#907) * feat: add checkExceeding function * test: add test cases of checkExceeding func * test: add more not increasing words --- String/CheckExceeding.js | 42 +++++++++++++++++++++++++++ String/test/CheckExceeding.test.js | 46 ++++++++++++++++++++++++++++++ 2 files changed, 88 insertions(+) create mode 100644 String/CheckExceeding.js create mode 100644 String/test/CheckExceeding.test.js diff --git a/String/CheckExceeding.js b/String/CheckExceeding.js new file mode 100644 index 000000000..1d2eb5506 --- /dev/null +++ b/String/CheckExceeding.js @@ -0,0 +1,42 @@ +/** + * @function checkExceeding + * @description - Exceeding words are words where the gap between two adjacent characters is increasing. The gap is the distance in ascii + * @param {string} str + * @returns {boolean} + * @example - checkExceeding('delete') => true, ascii difference - [1, 7, 7, 15, 15] which is incremental + * @example - checkExceeding('update') => false, ascii difference - [5, 12, 3, 19, 15] which is not incremental + */ +const checkExceeding = (str) => { + if (typeof str !== 'string') { + throw new TypeError('Argument is not a string') + } + + const upperChars = str + .toUpperCase() + .replace(/[^A-Z]/g, '') // remove all from str except A to Z alphabets + + const adjacentDiffList = [] + + for (let i = 0; i < upperChars.length - 1; i++) { + // destructuring current char & adjacent char by index, cause in javascript String is an object. + const { [i]: char, [i + 1]: adjacentChar } = upperChars + + if (char !== adjacentChar) { + adjacentDiffList.push( + Math.abs(char.charCodeAt() - adjacentChar.charCodeAt()) + ) + } + } + + for (let i = 0; i < adjacentDiffList.length - 1; i++) { + const { [i]: charDiff, [i + 1]: secondCharDiff } = adjacentDiffList + + if (charDiff > secondCharDiff) { + return false + } + } + + return true +} + +export { checkExceeding } diff --git a/String/test/CheckExceeding.test.js b/String/test/CheckExceeding.test.js new file mode 100644 index 000000000..f0ea4f084 --- /dev/null +++ b/String/test/CheckExceeding.test.js @@ -0,0 +1,46 @@ +import { checkExceeding } from '../CheckExceeding' + +describe('Testing CheckExceeding function', () => { + it('Testing the invalid types', () => { + expect(() => checkExceeding(Math.random())).toThrow('Argument is not a string') + expect(() => checkExceeding(null)).toThrow('Argument is not a string') + expect(() => checkExceeding(false)).toThrow('Argument is not a string') + expect(() => checkExceeding(false)).toThrow('Argument is not a string') + }) + + it('Testing with empty string', () => { + expect(checkExceeding('')).toBe(true) + }) + + it('Testing with linear alphabets', () => { + expect(checkExceeding('a b c d e ')).toBe(true) + expect(checkExceeding('f g h i j ')).toBe(true) + expect(checkExceeding('k l m n o ')).toBe(true) + expect(checkExceeding('p q r s t ')).toBe(true) + expect(checkExceeding('u v w x y z')).toBe(true) + }) + + it('Testing not exceeding words', () => { + expect(checkExceeding('Hello')).toBe(false) + expect(checkExceeding('world')).toBe(false) + expect(checkExceeding('update')).toBe(false) + expect(checkExceeding('university')).toBe(false) + expect(checkExceeding('dog')).toBe(false) + expect(checkExceeding('exceeding')).toBe(false) + expect(checkExceeding('resolved')).toBe(false) + expect(checkExceeding('future')).toBe(false) + expect(checkExceeding('fixed')).toBe(false) + expect(checkExceeding('codes')).toBe(false) + expect(checkExceeding('facebook')).toBe(false) + expect(checkExceeding('vscode')).toBe(false) + }) + + it('Testing exceeding words', () => { + expect(checkExceeding('bee')).toBe(true) // [ 3 ] + expect(checkExceeding('can')).toBe(true) // [ 2, 13 ] + expect(checkExceeding('good')).toBe(true) // [ 8, 11 ] + expect(checkExceeding('bad')).toBe(true) // [ 1, 3 ] + expect(checkExceeding('play')).toBe(true) // [ 4, 11, 24 ] + expect(checkExceeding('delete')).toBe(true) // [1, 7, 7, 15, 15] + }) +})