Javascript String Question 6

Introduction

Create a function which determines if the parameter is of an acceptable sequence.

The string parameter will be composed of + and = symbols with several letters between them.

For the string to be true each letter must be surrounded by a + symbol.

The string will not be empty and will have at least one letter.

i.e. "++d+===+c++==a" = "False"

function simpleSymbols(str) { 
    //your code//  w w  w.j  a  v  a 2 s .  c  o  m
}

// Output
console.log(simpleSymbols("+==f++d+"));
// => "False"

console.log(simpleSymbols("h+==f++d+"));
// => "False"

console.log(simpleSymbols("+==f++d+s"));
// => "False"

console.log(simpleSymbols("==+f+=+d+="));
// => "True"


function simpleSymbols(str) { 
    var result = "True";

    str = str.split('');
  
    for (var i = 0; i < str.length; i++) {
        if (str[i] === "=" || str[i] === "+") {
            continue;
        }
    
        if (str[i - 1] !== "+" || str[i + 1] !== "+") {
            result = "False";
        }
    }

    return result; 
}

// Output
console.log(simpleSymbols("+==f++d+"));
// => "False"

console.log(simpleSymbols("h+==f++d+"));
// => "False"

console.log(simpleSymbols("+==f++d+s"));
// => "False"

console.log(simpleSymbols("==+f+=+d+="));
// => "True"



PreviousNext

Related