'JavaScript function is always returning true where it should be false

My JavaScript function is always returning true whatever I write. The problem given wants me to validate that:

  1. space at beginning or at end are Not allowed.
  2. every character in the field must be a letter or a space (at most 1 space!) to seperate the words in the field (it's about palindrome words, i already wrote its function estPalindrome(word)).
  3. the button "chercher" must be enabled once 1 word is valid.

I have also tried to replace phrase and len inside and outside the function. The results are terrible:

Inside it only alerts "true" when I blur out with empty field outside it alerts "true" every time, whatever I write.

Note: I am NOT allowed to change anything in HTML code.

var phrase = document.getElementById('phrase').value;
var len = phrase.length;

function estPhrase() {
  var verify = true;

  for (i = 0; i < len; i++) {
    let ch = phrase.charAt(i);
    let isLetter = (ch >= 'a' && ch <= 'z') || (ch >= 'A' && ch <= 'Z') || ch == ' ';
    
    if (!isLetter || (ch == ' ' && phrase.charAt(i + 1) == ' ') || phrase.charAt(0) == ' ' || phrase.charAt(len - 1) == ' ')

      verify = false;
  }

  if (len > 0)
    butt.removeAttribute('disabled');

  var words = new Array();

  for (i = 0, j = 0; i < len; i++, j++) {
    ch = phrase.charAt(i);
    words[j] = "";

    while (ch != ' ') {
      words[j] += ch;
      i++;
    }

    if (estPalindrome(words[j]) == true)
      count++;
  }

  console.log(verify);
}
<div> Phrase: <input type="text" id="phrase" onblur="estPhrase()" size="50" /></div>


Solution 1:[1]

Modified a little and found it is working fine. working code

let phrase = 'abcd  lpp';
// let phrase = ' abcdlpp';
// let phrase = 'abcdlpp ';
// let phrase = 'abc,dlpp';

let len = phrase.length;

function estPhrase() {
  let verify = true;

  for (let i = 0; i < len; i++) {
    let ch = phrase.charAt(i);
    let isLetter = (ch >= 'a' && ch <= 'z') || (ch >= 'A' && ch <= 'Z') || ch === ' ';
    
    if (!isLetter || (ch === ' ' && phrase.charAt(i + 1) === ' ') || phrase.charAt(0) === ' ' || phrase.charAt(len - 1) === ' ')
      verify = false;
  }

  console.log(verify);
}

estPhrase(phrase)

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1