1

I'm attempting to solve the following problem from coderbyte.com:

Using the JavaScript language, 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.

The following is my attempt:

function SimpleSymbols(str) { 

  // code goes here  
var abc = 'abcdefghijklmnopqrstuvwxyz';

for (var i = 0; i < str.length; i++) {
    if (abc.indexOf(str[i]) !== -1) {
        if (str[i-1] + str[i+1] === "++") {
            return true;
        }
        else {
            return false;
        }
    }
}

}

This works for the following cases:

SimpleSymbols("+a+d+"); // true 
SimpleSymbols("+ab+d+"); // false
SimpleSymbols("b+d+"); // false

The only case I have found where this doesn't provide the right answer is when there is a trailing letter, for example:

SimpleSymbols("+a+b"); // true

This returns true, when in fact it should return false.

NB: I'm assuming string will be lowercase... I haven't dealt with case sensitivity, but I'd like to get the lowercase version working and then I will make it case independent.

Any ideas on what is wrong with my code?

0

5 Answers 5

1

I interpret this as: There is no letter which is not preceded/followed by a character other than plus (including none):

function SimpleSymbols(str) { 
  return !/^[a-z]|[^+][a-z]|[a-z][^+]|[a-z]$/i.test(str)
}
Sign up to request clarification or add additional context in comments.

1 Comment

This is awesome, but I haven't yet learned regular expressions!
1

You may use a regex:

    function SimpleSymbols(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; 
      }         
    }

or

function SimpleSymbols(str) { 

  var arr = str.toLowerCase().split("");
  for (var i = 0; i < arr.length; i++) {
    if (arr[i] >= "a" && arr[i] <= "z") {
      if (i === 0 || i === arr.length) {
        return false;
      }

      if (arr[i-1] !== "+" || arr[i+1] !== "+") {
        return false;
      }
    }
  }

  return true; 

}

Comments

0

This returns 'true' on the first successful letter surrounded by + symbols, it doesn't continue checking thru to the end.

I also wonder whether string indices would be out-of-bounds [i-1], [i+1] on the first/last character? It appears not, but I can't find a language ref.

Better:

function SimpleSymbols(str) { 
    var abc = 'abcdefghijklmnopqrstuvwxyz';

    for (var i = 0; i < str.length; i++) {
        if (abc.indexOf(str[i]) !== -1) {
            if (str[i-1] + str[i+1] != "++") {
                return false;
            }
        }
    }
    return true;
}

There are also probably performance improvements that could be made (checking bounding characters by AND rather than by string addition), and you could also do it by a regex.

By regex:

if (str.search( /[^+][a-z]/) >= 0)     // will match letters with no + beforehand.
    return false;
if (str.search( /[a-z][^+]/) >= 0)     // will match letters with no + afterward.
    return false;
return true;

2 Comments

Thanks for fixing my bad code, and for the suggestions on performance improvements. Is string concatenation generally bad from a performance perspective?
You're welcome! Generally, string concatenations aren't fast.. they need to allocate & copy a variable-length array, which is a lot slower than just testing/ or adding an integer in the processor. Don't worry about it, but if there's a simpler/faster alternative then prefer that. Hope this helps!
0

I believe this is a perfect regex case Check the regex ^[^a-z]*(\++[a-z]\++[a-z]?)*[^a-z]*$ explained in here

Use it in javascript like this:

function SimpleSymbols(str) { 
   return !/^[^a-z]*(\++[a-z]\++[a-z]?)*[^a-z]*$/i.test(str);
}

2 Comments

Regex still looks like an Alien language to me... haven't tackled learning it yet.
As you can see from the link it's explaining what's this expression means, you should start learning as it'll help you find any string patterns easily. Check regexpal.com to test your regex and check Regex Cheat sheet cheatography.com/davechild/cheat-sheets/regular-expressions
0
export const simpleSymbols = (str) => {
    const arr = str.split('')
    let k = []
    arr.forEach(element => {
        if((/[a-zA-Z]/).test(element)){
            if(arr[arr.indexOf(element)-1]==="+" && arr[arr.indexOf(element)+1]==="+"){
                k.push(1)
            }else{
                k.push(0)
            }
        }
    }); 

    if(k.includes(0)){
        return false
    }else{
        return true
    }

}

1 Comment

Please don't post only code as an answer, but also provide an explanation what your code does and how it solves the problem of the question. Answers with an explanation are usually more helpful and of better quality, and are more likely to attract upvotes.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.