Trying to improve my javascript code in this simple challenge from coderbyte Here is a slightly modified challenge from Coderbyte:

Determine if a given string is an acceptable. The str parameter will be composed of + and = symbols with several letters between them (ie. ++d+===+c++==a) and for the string to be true each letter must be surrounded by a + symbol. So the string to the left would be false. The string will not be empty and will have at least one letter. 

For whatever reason, I have struggled mightily with this challenge. I tried to use a regular expression in my Booleans, I tried cascading if statements, I tried a while statement surrounding if statements, I tried to structure my code as a function. After hours of failure, I finally resorted to the following:

var str = prompt("Please enter a test string: ").split("");
answer = "true";

if (((str[0] != "+") &amp;&amp; (str[0] != "=")) || ((str[str.length-1] != "+") &amp;&amp; (str[str.length-1] != "="))){
  answer = "false";
}

for (var i = 1; i &lt; str.length-1; i++){
  if (((str[i] != "+") &amp;&amp; (str[i] != "=")) &amp;&amp; (str [i-1] != "+" || str [i+1] != "+")){
      answer = "false";
  }
}  

console.log(answer);


I think it works, but of all the possible solutions to this problem, I am guessing that it is toward the bottom in terms of efficiency and elegance. I'd really appreciate some guidance on how this could be improved. 

Below is one of my "solutions" that did not work. Among other things, the Booleans are not evaluating correctly. I must be using the regular expressions incorrectly. 

var str = prompt("Please enter a test string: ").split("");
answer = "true";

if ((str[0] === /[a-z]/gi) || (str[str.length-1] === /[a-z]/gi)){
    answer = "false";
}
for (var i = 1; i &lt; str.length-1; i++){
  if (str[i] === /[a-z]/gi &amp;&amp; (str [i-1] != "+" || str [i+1] != "+")){
      answer = "false";
  }

}  

console.log(answer);
Your version is actually not that bad but could be simplified slightly and wrapped in a function. You could use charAt to get the character from a string instead of splitting the string. You don't need the initial check on the first and last characters; you can just do these as part of the main loop. (This works because str.charAt(-1) and str.charAt(str.length) both just return empty strings. Similarly if you were using an array, arr[-1] returns undefined.)

Wrapping this in a function and returning false when a non-matching character is found makes the code more efficient, as it does not have to run through the remaining loops when it already knows the answer (this would be the same as inserting break into your code.)

The below code also checks that the characters are either +, =, or a letter, which your original code does not do (so it would accept "===+!+==+3+", for example).

var str = "++d+===+c++=a";
console.log(checkString(str));

function checkString(str) {
    for (var i = 0; i &lt; str.length; i++) {
        if (str.charAt(i) != '+' &amp;&amp; str.charAt(i) != '=' &amp;&amp; 
            !(str.charAt(i) &gt;= 'a' &amp;&amp; str.charAt(i) &lt;= 'z' &amp;&amp; 
              str.charAt(i - 1) == '+' &amp;&amp; str.charAt(i + 1) == '+')) {
            return false;
        }
    }
    return true;
}


To use regular expressions you need to write pattern.test(str), e.g. /[a-z]/gi.test('a') returns true. However the point of using regex is that you can test a whole string in one go, rather than character-by-character. I think the following will work:

/^((\+[a-z])*\+|=)+$/gi.test('++d+===+c++=a')