I need to implement such isAlphabet function that will take letters and return true if the order of the letters matches the order in the alphabet, otherwise false:

isAlphabet ('abc') === true
isAlphabet ('aBc') === true
isAlphabet ('abd') === false // - there is c after b
isAlphabet ('a') === true
isAlphabet ('') === false // - task not completed
isAlphabet ('abcdefghjiklmnopqrstuvwxyz') === false // - j goes after i
isAlphabet ('tuvwxyz') === true
isAlphabet ('XYZ') === true
isAlphabet ('mnoprqst') === false // - q goes before r

My code:

function isAlphabet(letters) {
    // write code here
    const char = letters.toLowerCase();
    for (let i = 0; i < char.length; i++) {
        if (char[i + 1] - char[i] !== 1) {
            return false;
        }
    }
    return true;
}

For input 'abc', the function must return true but my implementation above returns false.

Could you help me to find an error in my code?


Edit

After having changed the code according to some suggestions to ...

function isAlphabet(letters) {
  // write code here
  const ch = letters.toLowerCase();
  for (let i = 0; i < ch.length; i++) {
    if (ch[i + 1].charCodeAt() - ch[i].charCodeAt() !== 1) {
      return false;
    }
  }
  return true;
}

... the function still errors.

2

There are 2 best solutions below

5
Peter Seliger On

From the above comments ...

"hint ... 'a'.charCodeAt(0) - 'b'.charCodeAt(0) equals -1 whereas 'a'.charCodeAt(0) - 'c'.charCodeAt(0) equals -2 and the result of 'aBc'.toLowerCase().split('') is ['a', 'b', 'c']." – Peter Seliger

"@PeterSeliger I have more than this one error. It is an example" – Vladyslav Akopov

"@VladyslavAkopov ... the hint already provides everything one needs for implementing a solution for a case insensitive detection of an alphabetic character sequence." – Peter Seliger

The next following (and commented) code example does prove the above claim.

function isStrictAlphabeticalAscendingSequence(value) {
  // always ensure a string value.
  value = String(value);
  return (
    // ensure a non-empty string value ...
    value.length >= 1 &&
    // ... and ...
    value
      // ... a case insensitive ...
      .toLowerCase()
      // ... character sequence (array) ...
      .split('')
      // ... where ...
      // (the usage of `every` guarantees an early exit)
      .every((char, idx, arr) => {
        let result = true;
        if (idx < arr.length - 1) {
          result =
            // ... each next character is a direct follower
            // of its previous character in a strict
            // alphabetic ascending meaning.
            (arr[idx + 1].charCodeAt(0) - char.charCodeAt(0) === 1);
        }
        return result;
      })
   );
}

[
  'abc',
  'aBc',
  'abd',
  'a',
  '',
  'abcdefghjiklmnopqrstuvwxyz',
  'tuvwxyz',
  'XYZ',
  'mnoprqst',
]
.forEach(value =>
  console
    .log(`'${ value }' => ${ isStrictAlphabeticalAscendingSequence(value) }`)
);
.as-console-wrapper { min-height: 100%!important; top: 0; }

0
Vitalii On
  function isAlphabet(letters) {
   
   const lowercaseLetters = letters.toLowerCase();

      // If the string has less than two characters, the order cannot 
 be determined
     // But if the input is a single character, it can be considered 
   an alphabet
    if (lowercaseLetters.length < 2) {
   return lowercaseLetters >= 'a' && lowercaseLetters <= 'z';
 }

   for (let i = 1; i < lowercaseLetters.length; i++) {
         // Compare the ASCII codes of the current letter and the 
  previous one
     if (lowercaseLetters.charCodeAt(i) < 
   lowercaseLetters.charCodeAt(i - 1)) {
  return false;
}
    // Check if the current letter is the same as the previous one
      // If it is, it's not an alphabet sequence

      if (lowercaseLetters[i] === lowercaseLetters[i - 1]) {
  return false;
}
    // Check if there are missing characters in the sequence

   if (lowercaseLetters.charCodeAt(i) - 
       lowercaseLetters.charCodeAt(i- 1) > 1) {
  return false;
}

}

return true; }