Skip to content

Instantly share code, notes, and snippets.

@emonidi
Created May 10, 2015 08:13
Show Gist options
  • Save emonidi/7b53e784578d77e3bde1 to your computer and use it in GitHub Desktop.
Save emonidi/7b53e784578d77e3bde1 to your computer and use it in GitHub Desktop.
Good JS Interview Task
/*
Have the function SimpleSymbols(str) take the str parameter being passed and determine if
it is an acceptable sequence by either returning the string true or false. 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.
*/
var correctString = '+d+=3=+s+';
var incorrectString = '+d=+3=2+'
function SimpleSymbols(str){
function symbolPositions(){
return str.match(/[\d,a-z]/ig);
}
function isNumber(symbol){
return parseInt(symbol);
}
function checkSurroundings(symbol,surroundingSign){
return str[str.indexOf(symbol) - 1] === surroundingSign && str[str.indexOf(symbol) + 1] === surroundingSign;
}
var positions = symbolPositions();
for(var i = 0; i < positions.length; i++){
if(isNumber(positions[i])){
if(!checkSurroundings(positions[i],'=')){
return false;
}
}else{
if(!checkSurroundings(positions[i],'+')){
return false;
}
}
}
return true;
}
console.log(SimpleSymbols(correctString));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment