5/5 characters left. Any reason for the extra variable?


In this exercise we are told to create two new variables, postLength and charactersLeft.
To me however charactersLeft seem redundant.

Instead of this:

  $('.status-box').keyup(function() {
    var postLength = $(this).val().length;
    var charactersLeft = 140 - postLength;

We can just write:

$('.status-box').keyup(function() {
var postLength = $(this).val().length;
$('.counter').text(140 - postLength);

Or am I missing something? What are the disadvantages of doing it this way?


Hey Io,

You're absolutely correct that you could just use 140 - postLength instead of creating an entire variable for it. The thing is, you'll be using charactersLeft multiple times, and if you're using something two or more times, it's usually better to create a variable for it :slight_smile:


This topic was automatically closed 24 hours after the last reply. New replies are no longer allowed.