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

Working on at outside project cause I need a break from the jquery course. Getting a var not defined error

It seems to work as intended until I add the if statement near the bottom

var main = function(){
  $(".btn").click(function(){ /* .val() like.text() for form inputs */
    var post = $(".status-box").val(); /* entered text stored in post */
    $("<li>").text(post).prependTo(".posts"); /*create li + txt + prepend*/ 
    $(".status-box").val(""); /* clear status box */
    $('.counter').text('140'); /* reset counter at btn click */
  });

  $(".status-box").keyup(function(){ /* count at each upstroke */
  var postLength = $(this).val().length; /* char in status-box */
  var charectersLeft = 140 - postLength; /* total - strokes */
  $(".counter").text(charectersLeft); /* display total */
  // this works fine until if statement
  // then I get an error charactersLeft not defined
  // maybe be something else I'm missing
  // cause charactersLeft seems to be working 
  // prior to addition of the if statement
  if(charactersLeft < 0){
      $(".btn").addClass("disabled");
    } else if(charactersLeft === 140){ 
      $(".btn").addClass("disabled");
    } else{
      $(".btn").removeClass("disabled");
    }
  }); /* keyup event */

  // $(".btn").addClass("disabled");

}

3 Answers

Jennifer Nordell
seal-mask
STAFF
.a{fill-rule:evenodd;}techdegree
Jennifer Nordell
Treehouse Teacher

Hi there! You've been stung by the typo monster!

var charectersLeft = 140 - postLength; /* total - strokes */
  $(".counter").text(charectersLeft); 

Here, you've spelled it "charecters" instead of "characters" in two places :smiley: But then you have it spelled correctly here:

 if(charactersLeft < 0){
      $(".btn").addClass("disabled");
    } else if(charactersLeft === 140){ 

Hope this helps! :sparkles:

Thanks, I thought i used copy and paste but apparently not.

Should this statement below have == and not === ?

else if(charactersLeft === 140){ 

They use == but I thought === would be safe to use

Only thing i see at first glance is a simple typo

var charectersLeft = 140 - postLength  // typo on word 'characters' 

Thank you