Skip to content

Instantly share code, notes, and snippets.

@MorenoMdz
Created May 5, 2018 21:40
Show Gist options
  • Save MorenoMdz/b8f8e0820639daa44ef5e4ee28297095 to your computer and use it in GitHub Desktop.
Save MorenoMdz/b8f8e0820639daa44ef5e4ee28297095 to your computer and use it in GitHub Desktop.
/**
*
This challenge requires you to determine if every alphabetic character in a string has a plus (+) symbol on the left and right side of itself. For example: the string "+a+b+c+" is good but the string "===+3+f=+b+" is not because the "f" does not have a plus symbol to its right. A very simple way to solve this challenge is to create a loop that every time it gets to an alphabetic character, it checks to see if it is surrounded by + symbols.
*/
/* Not pure f */
function SimpleSymbols(str) {
var str = "=" + str + "=";
for (var i = 0; i < str.length; i++) {
if (str[i].match(/[a-z]/i) !== null) {
// -1 to begging and + 1 to end
if (str[i - 1] !== "+" || str[i + 1] !== "+") {
return false;
}
}
}
return true;
}
/* Else If & Regex test() */
function SimpleSymbols2(str) {
if (/^[a-zA-Z]/.test(str) || /[a-zA-Z]$/.test(str)) {
return false;
} else if (/[^+][a-zA-Z]/.test(str) || /[a-zA-Z][^+]/.test(str)) {
return false;
} else {
return true;
}
}
/* heavy comparing conditional */
function SimpleSymbols(str) {
let answer = "true";
let string = "=" + str + "=";
for (let i = 1; i < string.length - 1; i++) {
if (
string[i].match(/[a-z]/i) !== null &&
(string[i - 1] !== "+" || string[i + 1] !== "+")
) {
answer = "false";
}
}
return answer;
}
/* Split */
function SimpleSymbols(str) {
var ref;
if (str.match(/\+\w\+/g) === null) {
return false;
} else {
ref = str
.split("")
.map(function(e, i, arr) {
if (/[a-z]/g.test(e)) {
return arr[i - 1] === "+" && arr[i + 1] === "+" ? "o" : "x";
} else {
return "x";
}
})
.join("");
return ref.match(/o/g).length === str.match(/[a-z]/g).length;
}
}
/* subtstrings */
function SimpleSymbols(str) {
if(str.charAt(0).match(/[a-z]/i)) return false;
if(str.charAt(str.length-1).match(/[a-z]/i)) return false;
for (j=1;j<str.length;j++){
if(str.charAt(j).match(/[a-z]/i) && (str.substr(j-1,1) != "+" || str.substr(j+1,1) != "+") )
return false;
}
return true;
}
SimpleSymbols(readline());
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment