Files
JavaScript/String/CheckExceeding.js
Roland Hummel 86d333ee94 feat: Test running overhaul, switch to Prettier & reformat everything (#1407)
* chore: Switch to Node 20 + Vitest

* chore: migrate to vitest mock functions

* chore: code style (switch to prettier)

* test: re-enable long-running test

Seems the switch to Node 20 and Vitest has vastly improved the code's and / or the test's runtime!

see #1193

* chore: code style

* chore: fix failing tests

* Updated Documentation in README.md

* Update contribution guidelines to state usage of Prettier

* fix: set prettier printWidth back to 80

* chore: apply updated code style automatically

* fix: set prettier line endings to lf again

* chore: apply updated code style automatically

---------

Co-authored-by: github-actions <${GITHUB_ACTOR}@users.noreply.github.com>
Co-authored-by: Lars Müller <34514239+appgurueu@users.noreply.github.com>
2023-10-04 02:38:19 +05:30

41 lines
1.3 KiB
JavaScript

/**
* @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 }