Skip to content
Closed
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
33 changes: 33 additions & 0 deletions String/CheckExceedingString.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
// checks if a string is exceeding string or not
// Exceeding words are words where the gap between two adjacent characters is increasing.
// The gap is the distance in ascii
// More info at : https://github.com/TheAlgorithms/Javascript/issues/765

/**
* a function to check whether a string is exceedng or not
* @param str the str that should be checked
*/
function checkExceedingString(str) {
if (typeof str !== 'string') throw new TypeError('str must be a string!')

const asciiArr = []

// collect the ascii of each letter
for (let s of str) asciiArr.push(s.charCodeAt())

let isExceeding = true

for (let j = 0; j < asciiArr.length - 3; j++) {
// if the ascii of current letter is greater than the next letter it's not an exceeding string
let gapCond =
Math.abs(asciiArr[j] - asciiArr[j + 1]) >
Math.abs(asciiArr[j + 2] - asciiArr[j + 3])
if (gapCond) {
isExceeding = false
break
}
}
return isExceeding
}

module.exports = checkExceedingString
19 changes: 19 additions & 0 deletions String/test/CheckExceedingString.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
const checkExceedingString = require('./CheckExceedingString')

describe('check exceeding string', () => {
it('expect to throw an error if input is not a string', () => {
expect(() => checkExceedingString(null)).toThrow()
})

it('expects to return true if the input is exceeding string', () => {
const value = 'abcdef'
const result = checkExceedingString(value)
expect(result).toBe(true)
})

it('expects to return false if the input is not in camel case format', () => {
const value = 'happy'
const result = checkExceedingString(value)
expect(result).toBe(false)
})
})