Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

JavaScript

My code finds 'smiley faces' in arrays, but it won't pass all the tests. Can you see why some tests fail?

I'm working on a challenge to count the number of smiley faces ;) in an array. The requirements are:

-Each smiley face must contain a valid pair of eyes. Eyes can be marked as : or ;
-A smiley face can have a nose but it does not have to. Valid characters for a nose are - or ~
-Every smiling face must have a smiling mouth that should be marked with either ) or D.
No additional characters are allowed except for those mentioned.
Valid smiley face examples:
:) :D ;-D :~)
Invalid smiley faces:
;( :> :} :] 

So, this is the code I came up with:

//var arr = []; //should equal 0, and does equal 0

//var arr = [':)',':(',':D',':O',':;']; //should equal 2, BUT equals 5

//var arr = [':D',':~)',';~D',':)'] //should equal 4, and does equal 4

//var arr = [';]', ':[', ';*', ':$', ';-D'] //should equal 1 , BUT equals 3

var count = 0;

function countSmileys(arr) {

  for ( i = 0; i < arr.length; i++) {

    var testSmile = arr[i];

    //if string length is 2...
    if ( testSmile.length === 2 ) {
      //check that it's a valid smiley 
      if ( testSmile.charAt(0) === ':' || testSmile.charAt(0) === ';' 
      && testSmile.charAt(1) === ')' || testSmile.charAt(1) === 'D' ) {

        count++;

      }

    }

    //if string length is 3...
    if ( testSmile.length === 3 ) {
      //check that it's a valid smiley 
      if ( testSmile.charAt(0) === ':' || testSmile.charAt(0) === ';' 
      && testSmile.charAt(1) === '-' || testSmile.charAt(1) === '~' 
      && testSmile.charAt(2) === ')' || testSmile.charAt(2) === 'D' ) {

        count++;

      }

    }
    //if the array is empty, return 0 - another requirement of the challenge
    if ( arr.length === 0 ) {

      count = 0;

    }

  } //close for loop

  return count;

} //close function

countSmileys(arr);

Also available in a more readable and interactive format here: https://repl.it/JwHo/3

So, it seems to work for only 2 of the 4 tests, and it seems that my code counts more as valid than there should be... can you see what's getting through? Or is there a more macro-level problem with the way I'm counting?

Thanks!

2 Answers

Steven Parker
Steven Parker
242,191 Points

It looks like your issue is operator precedence. If you want OR operations ("||") to be performed before AND ("&&"), you'll need to group them using parentheses. Otherwise the AND operations will be done first.

Oh! Of course I should be grouping those! I actually didn't know I had to do that, but it makes sense. I think the way I put them on separate lines obscured that problem for me.

Thanks!