MCPcopy Create free account
hub / github.com/TheAlgorithms/JavaScript / checkExceeding

Function checkExceeding

String/CheckExceeding.js:9–38  ·  view source on GitHub ↗
(str)

Source from the content-addressed store, hash-verified

7 * @example - checkExceeding('update') => false, ascii difference - [5, 12, 3, 19, 15] which is not incremental
8 */
9const checkExceeding = (str) => {
10 if (typeof str !== 'string') {
11 throw new TypeError('Argument is not a string')
12 }
13
14 const upperChars = str.toUpperCase().replace(/[^A-Z]/g, '') // remove all from str except A to Z alphabets
15
16 const adjacentDiffList = []
17
18 for (let i = 0; i < upperChars.length - 1; i++) {
19 // destructuring current char & adjacent char by index, cause in javascript String is an object.
20 const { [i]: char, [i + 1]: adjacentChar } = upperChars
21
22 if (char !== adjacentChar) {
23 adjacentDiffList.push(
24 Math.abs(char.charCodeAt() - adjacentChar.charCodeAt())
25 )
26 }
27 }
28
29 for (let i = 0; i < adjacentDiffList.length - 1; i++) {
30 const { [i]: charDiff, [i + 1]: secondCharDiff } = adjacentDiffList
31
32 if (charDiff > secondCharDiff) {
33 return false
34 }
35 }
36
37 return true
38}
39
40export { checkExceeding }

Callers 1

Calls 1

pushMethod · 0.45

Tested by

no test coverage detected