Skip to main content
edited body
Source Link
Malachi
  • 29.1k
  • 11
  • 87
  • 188
$("button").click(function(){
    if(clicked){
        $(this).css('background-color', 'red');
        clicked  = false;
    } else {
        $(this).css('background-color', 'blue');
        clicked  = true;
    }   
});

You could do it like this instead

$("button").click(function(){
    var color = clicked ? 'red' : 'blue';
    $(this).css('background-color', color);
    clicked = !clicked;
});

We move the color picking to a single variable choice using a ternary statement and then we only have to write out the change to the CSS of the element once. then we flip the boolean.

I don't like the name of the boolean variable, it doesn't accurately describe what it is keeping track of, based on the way the code is written it should be named isButtonBlue.

If the button is blue, turn it red. ifIf the button is not blue, turn it blue.


After looking at this a little bit longer I was thinking that you could make it one line shorter by making another line longer by moving the ternary statement into the CSS change

$("button").click(
    $(this).css('background-color', isButtonBlue ? 'red' : 'blue');
    isButtonBlue = !isButtonBlue;
});
$("button").click(function(){
    if(clicked){
        $(this).css('background-color', 'red');
        clicked  = false;
    } else {
        $(this).css('background-color', 'blue');
        clicked  = true;
    }   
});

You could do it like this instead

$("button").click(function(){
    var color = clicked ? 'red' : 'blue';
    $(this).css('background-color', color);
    clicked = !clicked;
});

We move the color picking to a single variable choice using a ternary statement and then we only have to write out the change to the CSS of the element once. then we flip the boolean.

I don't like the name of the boolean variable, it doesn't accurately describe what it is keeping track of, based on the way the code is written it should be named isButtonBlue.

If the button is blue, turn it red. if the button is not blue, turn it blue.


After looking at this a little bit longer I was thinking that you could make it one line shorter by making another line longer by moving the ternary statement into the CSS change

$("button").click(
    $(this).css('background-color', isButtonBlue ? 'red' : 'blue');
    isButtonBlue = !isButtonBlue;
});
$("button").click(function(){
    if(clicked){
        $(this).css('background-color', 'red');
        clicked  = false;
    } else {
        $(this).css('background-color', 'blue');
        clicked  = true;
    }   
});

You could do it like this instead

$("button").click(function(){
    var color = clicked ? 'red' : 'blue';
    $(this).css('background-color', color);
    clicked = !clicked;
});

We move the color picking to a single variable choice using a ternary statement and then we only have to write out the change to the CSS of the element once. then we flip the boolean.

I don't like the name of the boolean variable, it doesn't accurately describe what it is keeping track of, based on the way the code is written it should be named isButtonBlue.

If the button is blue, turn it red. If the button is not blue, turn it blue.


After looking at this a little bit longer I was thinking that you could make it one line shorter by making another line longer by moving the ternary statement into the CSS change

$("button").click(
    $(this).css('background-color', isButtonBlue ? 'red' : 'blue');
    isButtonBlue = !isButtonBlue;
});
added 160 characters in body
Source Link
Malachi
  • 29.1k
  • 11
  • 87
  • 188
$("button").click(function(){
    if(clicked){
        $(this).css('background-color', 'red');
        clicked  = false;
    } else {
        $(this).css('background-color', 'blue');
        clicked  = true;
    }   
});

You could do it like this instead

$("button").click(function(){
    var color = clickerclicked ? 'red' : 'blue';
    $(this).css('background-color', color);
    clickerclicked = !clicker;clicked;
});

We move the color picking to a single variable choice using a ternary statement and then we only have to write out the change to the CSS of the element once. then we flopflip the clickerboolean.

I changeddon't like the variable name becauseof the boolean variable, it sounded right to say clicker insteaddoesn't accurately describe what it is keeping track of clicked, I thinkbased on the way the code is written it makes senseshould be named isButtonBlue.

If the button is blue, turn it red. if the button is not blue, turn it blue.


After looking at this a little bit longer I was thinking that you could make it one line shorter by making another line longer by moving the ternary statement into the CSS change

$("button").click(
    $(this).css('background-color', clickerisButtonBlue ? 'red' : 'blue');
    clickerisButtonBlue = !clicker;isButtonBlue;
});
$("button").click(function(){
    if(clicked){
        $(this).css('background-color', 'red');
        clicked  = false;
    } else {
        $(this).css('background-color', 'blue');
        clicked  = true;
    }   
});

You could do it like this instead

$("button").click(function(){
    var color = clicker ? 'red' : 'blue';
    $(this).css('background-color', color);
    clicker = !clicker;
});

We move the color picking to a single variable choice using a ternary statement and then we only have to write out the change to the CSS of the element once. then we flop the clicker.

I changed the variable name because it sounded right to say clicker instead of clicked, I think it makes sense.


After looking at this a little bit longer I was thinking that you could make it one line shorter by making another line longer by moving the ternary statement into the CSS change

$("button").click(
    $(this).css('background-color', clicker ? 'red' : 'blue');
    clicker = !clicker;
});
$("button").click(function(){
    if(clicked){
        $(this).css('background-color', 'red');
        clicked  = false;
    } else {
        $(this).css('background-color', 'blue');
        clicked  = true;
    }   
});

You could do it like this instead

$("button").click(function(){
    var color = clicked ? 'red' : 'blue';
    $(this).css('background-color', color);
    clicked = !clicked;
});

We move the color picking to a single variable choice using a ternary statement and then we only have to write out the change to the CSS of the element once. then we flip the boolean.

I don't like the name of the boolean variable, it doesn't accurately describe what it is keeping track of, based on the way the code is written it should be named isButtonBlue.

If the button is blue, turn it red. if the button is not blue, turn it blue.


After looking at this a little bit longer I was thinking that you could make it one line shorter by making another line longer by moving the ternary statement into the CSS change

$("button").click(
    $(this).css('background-color', isButtonBlue ? 'red' : 'blue');
    isButtonBlue = !isButtonBlue;
});
deleted 1 character in body
Source Link
Malachi
  • 29.1k
  • 11
  • 87
  • 188
$("button").click(function(){
    if(clicked){
        $(this).css('background-color', 'red');
        clicked  = false;
    } else {
        $(this).css('background-color', 'blue');
        clicked  = true;
    }   
});

You could do it like this instead

$("button").click(function(){
    var color = clicker ? 'red' : 'blue';
    $(this).css('background-color', color);
    clicker != !clicker;
});

weWe move the color picking to a single variable choice using a ternary statement and then we only have to write out the change to the CSS of the element once. then we flop the clicker.

I changed the variable name because it sounded right to say clicker instead of clicked. I, I think it makes sense.


After looking at this a little bit longer I was thinking that you could make it one line shorter by making another line longer by moving the ternary statement into the CSS change

$("button").click(
    $(this).css('background-color', clicker ? 'red' : 'blue');
    clicker != !clicker;
});
$("button").click(function(){
    if(clicked){
        $(this).css('background-color', 'red');
        clicked  = false;
    } else {
        $(this).css('background-color', 'blue');
        clicked  = true;
    }   
});

You could do it like this instead

$("button").click(function(){
    var color = clicker ? 'red' : 'blue';
    $(this).css('background-color', color);
    clicker != clicker;
});

we move the color picking to a single variable choice using a ternary statement and then we only have to write out the change to the CSS of the element once. then we flop the clicker.

I changed the variable name because it sounded right to say clicker instead of clicked. I think it makes sense.


After looking at this a little bit longer I was thinking that you could make it one line shorter by making another line longer by moving the ternary statement into the CSS change

$("button").click(
    $(this).css('background-color', clicker ? 'red' : 'blue');
    clicker != clicker;
});
$("button").click(function(){
    if(clicked){
        $(this).css('background-color', 'red');
        clicked  = false;
    } else {
        $(this).css('background-color', 'blue');
        clicked  = true;
    }   
});

You could do it like this instead

$("button").click(function(){
    var color = clicker ? 'red' : 'blue';
    $(this).css('background-color', color);
    clicker = !clicker;
});

We move the color picking to a single variable choice using a ternary statement and then we only have to write out the change to the CSS of the element once. then we flop the clicker.

I changed the variable name because it sounded right to say clicker instead of clicked, I think it makes sense.


After looking at this a little bit longer I was thinking that you could make it one line shorter by making another line longer by moving the ternary statement into the CSS change

$("button").click(
    $(this).css('background-color', clicker ? 'red' : 'blue');
    clicker = !clicker;
});
added 309 characters in body
Source Link
Malachi
  • 29.1k
  • 11
  • 87
  • 188
Loading
Source Link
Malachi
  • 29.1k
  • 11
  • 87
  • 188
Loading