Reputation: 39
so there's this exercise on coderbyte that goes:
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 characters 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.
sample test cases:
Input:"+d+=3=+s+"
Output:true
Input:"f++d+"
Output:false
and here is the solution that i came up with, but it's not working:
function SimpleSymbols(str) {
// code goes here
var splitted = str.split('')
var result = splitted.map(function (arr){
for (var i=0; i<splitted.length; i++){
if (splitted[i] == '+' && splitted[i+2] == '+' && splitted[i+1] == /[a-z]/gi || && splitted[i+1] == /[A-Z]/gi){
return true
}
return false
}
}
})
return result;
}
// keep this function call here
SimpleSymbols(readline());
i believe the problem lies within how I wrote the regex condition on the if statement.
so how is the right way to found out that splitted[i+1] is a letter?
Upvotes: 1
Views: 3126
Reputation: 500
I believe this is the simplest solution. Just walk the string and check if each found letter is surrounded by +
on both sides.
function isEachLetterSurroundedByPlus(str) {
const re = /[a-z]/i;
for (let i = 0; i < str.length; i++) {
if (re.test(str[i])) {
if (str[i - 1] !== "+" || str[i + 1] !== "+")
return false;
}
}
return true;
}
console.log(isEachLetterSurroundedByPlus("+s+f+=+d+d=d+"));
console.log(isEachLetterSurroundedByPlus("+s+f+=+d+d+=+d+"))
Upvotes: 0
Reputation: 1555
You may search for a charecter surrounded by something different than the plus symbol and return false when you find it. Something like this:
function isValid(str) {
return !str.match(/\+[a-z]([^+]|$)|([^+]|^)[a-z]([^+]|$)|([^+]|^)[a-z]\+/gi);
}
isValid("+d+=3=+s+"); /// --> true
isValid("f++d+"); /// --> false
Upvotes: 0
Reputation: 37755
You're for loop on splittedArray inside map doesn't make sense, you can directly use index parameter of map
`.map(currentValue, index, array)`
And also you're not using regex to test string, you're comparing string with regex,
`splitted[i+1] == /[a-z]/gi`
if you want to use regex you can use this pattern
(?:^|[^+])[a-z]|[a-z](?:[^+]|$)
let inputs = ["+d+=3=+s+","f++d+"]
let check = (str) => +(str.match(/(?:^|[^+])[a-z]|[a-z](?:[^+]|$)/gi) || []).length > 0
inputs.forEach(str=> console.log(check(str)))
Upvotes: 1
Reputation: 377
function SimpleSymbols(str) {
// code goes here
var splitted = str.split('')
var result = splitted.map(function (arr){
for (var i=0; i<splitted.length; i++){
if (splitted[i] == '+' && splitted[i+2] == '+' &&
(/^[a-zA-Z]+$/.test(splitted[i+1])))
{
console.log('true');
return true;
}else{
console.log('false');
return false;
}
}
})
return result;
}
SimpleSymbols('+d+=3=+s+');
Upvotes: 1
Reputation: 1811
Something like this
function SimpleSymbols(str) {
// code goes here
var splitted = str.split('+')
var result = splitted.map(function (arr){
for (var i=0; i < splitted.length; i++){
if ( /[a-z]/gi.test(splitted[i]) && /[A-Z]/gi.test(splitted[i+1])
){
return true
}
return false
}
})
return result;
}
// keep this function call here
console.log(SimpleSymbols('f+g+j'));
Upvotes: 1