Having trouble solving this JavaScript coding challenge from coderbyte

Related searches

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?

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)
}

Coderbyte, Improve your coding skills with our library of 300+ challenges and prepare for coding interviews with content from leading Learn JavaScript in One Week. Having trouble solving this JavaScript coding challenge from coderbyte. Trouble with Javascript easy coderbyte challenge. 1. Coderbyte Letter Changing challenge

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;

Challenges, Improve your coding skills with our library of 300+ challenges and prepare for coding interviews with content NEW Node.js Write Stream View video solution. CoderByte is one of the most popular coding challenge websites in the world. It contains a huge variety of different exercises, each of which is designed to test different concepts or coding syntax. However, the thing that gets CoderByte a place on this list is the fact that the majority of their JavaScript programming exercises are unique.

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);
}

Coderbyte offer a mix of free and paid resources and challenges for software engineers looking to practice their skills. Read more about Coderbyte in our review. With exercises in Python and JavaScript for beginners and advanced engineers, CheckiO is a good free resource to check out.

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; 

}

These are all relatively minor notes: You can use typing.List to provide a better type definition for the parameter (List[str]), precisely defining what it's a list of.. I always try to avoid giving variables names that are just a variation on the Python type to tell me what its type is (that's the type annotation's job); the problem description calls this the "matrix" so I'll use that.

Interviewers sometimes ask simpler programming questions to make sure you have basic coding skills and competency. Below are links to challenges from Programming Practice that you should be able to find optimal solutions for. (Some of the links below are to a final challenge within a series of challenges, but you can go through the challenges

The thing is one which is best for others might not be good for you. You have your own likes and dislikes. So i am not going to tell you the best but i will give you some details based on which you can choose your best.

I have been learning about front-end development for a while now. I'm still having trouble coding from scratch. For instances, I was doing the Student Record Challenge from the Javascript Arrays, Loops, and Objects course, even with understanding the different methods, I was still at a blank when building the challenge Dave gave us.

Comments
  • This is awesome, but I haven't yet learned regular expressions!
  • 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!
  • 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