These are chat archives for FreeCodeCamp/Help

20th
Jan 2015
Christopher Hutchinson
@cshutchinson
Jan 20 2015 02:08
nathan can you help me with a regex?
James McShane
@jmcshane
Jan 20 2015 02:09
@cshutchinson what's the regex you are working on?
Christopher Hutchinson
@cshutchinson
Jan 20 2015 02:10
I want to find and letters proceeded and followed by + like /+\w+/ but not a string without that condition holding true for entire string
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.
James McShane
@jmcshane
Jan 20 2015 02:12
ah, so you want to test if there is any letter not surrounded by +, right?
Christopher Hutchinson
@cshutchinson
Jan 20 2015 02:12
yeah, but I can't figure it out with just regex....is that possible?
I have been on this WAY TOO LONG
learned alot but no dice yet
Justin Rogers
@yaskyj
Jan 20 2015 02:13
Earlier today it was a combination of regex, a for loop, and if statement.
Christopher Hutchinson
@cshutchinson
Jan 20 2015 02:14
ok gotcha, I can do that
ty
wanted to make sure I couldn't qualify an entire string with regex alone
Justin Rogers
@yaskyj
Jan 20 2015 02:14
I'm sure there are other ways. That's just what the discussion was about a few hours ago.
James McShane
@jmcshane
Jan 20 2015 02:14
yeah, i don't think you can do it with just a single regex match
Christopher Hutchinson
@cshutchinson
Jan 20 2015 02:15
i sure learned alot trying LOL
ty @jmcshane
I appreciate your help
James McShane
@jmcshane
Jan 20 2015 02:15
what i would do maybe, is remove all the things that match /+[\w]++/ and then see if there are any characters left in the string
Ben Clist
@BenClist
Jan 20 2015 02:15
You probably could do it with a Regex but it would be really complex, much simpler to do it the other way
Christopher Hutchinson
@cshutchinson
Jan 20 2015 02:16
ty both :)
Christopher Hutchinson
@cshutchinson
Jan 20 2015 05:07
([^+][a-z])|([a-z][^+]) is the regex to find the issues but I can't take credit for it...damn wish I was smarter
function SimpleSymbols(str) { 

  // code goes here
  var re = /[a-zA-Z]/g;
  var letterLocations = [ ];
  var tempResult = false;
  for (i=0; i<str.length; i++){
    if (re.test(str.charAt(i))==true){
      letterLocations.push(i);
    }
  }
  for (i=0; i<letterLocations.length; i++){
      if (str.charAt(letterLocations[i]-1) ==='+' && str.charAt(letterLocations[i]+1) ==='+'){
        tempResult = true;
      }else{
        tempResult = false;
        return str=false;
      }
  } 
  str=tempResult;
  return str; 

}
I wrote a novel as you can see as usual
Ben Clist
@BenClist
Jan 20 2015 05:12
that looks quite complex lol :)
function SimpleSymbols(str) { 

  for(i = 0; i < str.length; i++) { //Loop through the string
    if(/[a-zA-Z]/.test(str[i])) { //Check if the current string index is a letter
      if(str[i-1] != '+' || str[i+1] != '+') { //Check if the index is surrounded by +
        return false; //if not return false
      };
    }; 
  };
  return true; //If we get to this point we haven't encountered a letter not surrounded by + so we can return true      
}
That's the commented version of mine
My first version looked like yours, that's the benefit of going back after doing the other challenges
Ben Clist
@BenClist
Jan 20 2015 05:17
you learn so much just by battling through getting solutions for them that when you go back to the early ones you wonder how you ever found them difficult
Christopher Hutchinson
@cshutchinson
Jan 20 2015 11:41
@BenClist thank you for posting that :) helps me to see a simpler version ....faux pair programming :)
Alex Olteanu
@alexandruolteanu
Jan 20 2015 18:23
Any nginx guru here?
Evan Davis
@davisec52
Jan 20 2015 19:43

I've been working on the coderbyte challenge to return true or false for a string comprising only "=", "+", and single alpha characters such that each alpha character is surrounded by "+".

The first code sample shows my solution, which has the return true statement in the "for" loop, separate from the conditional statements. This appears to satisfy the requirements of the challenge.

My question--sorry for being long winded--is why does the return true statement have to be outside of the conditional statements in order to give the correct results?

The input below should return false.


function sym(str) {
    var pattern = /[a-zA-Z]/g;
    for(var i = 0; i < str.length; i++) {
        if(str[i].match(pattern)) {
            if(str[i + 1] !== "+" || str[i - 1] !== "+") {
                return false
            }
        }
        else if(str[0] !== "=" && str[0] !== "+" || str[str.length -1] !== "+" && str[str.length -1] !== "=") {
            return false;
        }
        else if(str[i] !== "+" && str[i] !== "=") {
            return false;
        }
        //else {return true;}  //<== Returns wrong results per requirements of challenge
    }
    return true;               //<== Returns CORRECT results per challenge stipulations
}
sym("=+t+=++===99=+p+==");
Ryan Malm
@Rybar
Jan 20 2015 20:04
your logic isn't discerned until after your for loop has completed running. Your "truthy" statement is getting through the for loop without returning false.
Evan Davis
@davisec52
Jan 20 2015 20:07
@Rybar Gotcha! Thank you.
Christopher Hutchinson
@cshutchinson
Jan 20 2015 21:28
@BenClist Sure nice to hear that....feeling like an idiot on some of these coderbytes....taking much longer than I anticipate
James McShane
@jmcshane
Jan 20 2015 22:55
@cshutchinson I had the same feeling during the challenges, that I couldn't do anything. Then, I stopped programming in their window which allowed me to test stuff in isolation more. Doing the challenges small pieces at a time can really help