Skip to content

Instantly share code, notes, and snippets.

@jczaplew
Last active January 4, 2016 00:49
Show Gist options
  • Save jczaplew/8544586 to your computer and use it in GitHub Desktop.
Save jczaplew/8544586 to your computer and use it in GitHub Desktop.
If/else
if (something === something) {
if (somethingelse === somethingelse) {
// somethingelse equals somethingelse
} else {
// somethingelse isn't equal to somethingelse
}
} else {
// something doesn't equal something
}
// What you had was...
if (a === b) {
// do something
}
if (c === d) {
// do something else
}
// in that case, two separate things will happen
// This is what you originally had
var compare = function(choice1, choice2) {
if (choice1 === choice2) {
return("The result is a tie!");
}
if (choice1 === "rock") {
if (choice2 === "scissors") {
return("Rock wins!");
} else {
return("Paper wins!");
}
}
// Now we have
var compare = function(choice1, choice2) {
if (choice1 === choice2) {
return("The result is a tie!");
} else if (choice1 === "rock") {
if (choice2 === "scissors") {
return("Rock wins!");
} else {
return("Paper wins!");
}
}
@laurengoods
Copy link

Now we are here:

var compare = function(choice1, choice2)
{ if (choice1 === choice2) {
    return("The result is a tie!"); }
else if (choice1 === "rock") {
    if (choice2 === "scissors") {
        return("Rock wins!");}
    else {
        return("Paper wins!");} 
}

Here we go! (I think...)

var compare = function(choice1, choice2)
    { if (choice1 === choice2) {
        return("The result is a tie!"); 
    } else if (choice1 === "rock")
        if (choice2 === "scissors") {
            return("Rock wins!");
        } else {
            return("Paper wins!");
        } 
    };

And again, with more...

var compare = function(choice1, choice2)   
{ if (choice1 === choice2) {
    return("The result is a tie!"); 
} else if (choice1 === "rock") {
    if (choice2 === "scissors") {
        return("Rock wins!");
    } else {
        return("Paper wins!");
    }
} else if (choice1 === "paper") {
        if (choice2 ==="rock") {
            return("Paper wins!");
    } else {
        return("Scissors wins!");
    }
} else if (choice1 === "scissors") {
        if (choice2 === "rock") {
            return("Rock wins!");
        } else {
            return("Scissors wins!");
    }
};

@jczaplew
Copy link
Author

Let's clean it up a bit

var compare = function(choice1, choice2)   { 
    if (choice1 === choice2)  {
        return("The result is a tie!"); 
    } else if (choice1 === "rock")  {
        if (choice2 === "scissors")  {
            return("Rock wins!");
        } else {
            return("Paper wins!");
        }
    } else if (choice1 === "paper")  {
        if (choice2 ==="rock")  {
            return("Paper wins!");
        } else {
            return("Scissors wins!");
        }
    } else if (choice1 === "scissors")  {
        if (choice2 === "rock") {
            return("Rock wins!");
        } else {
            return("Scissors wins!");
        }
    };

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment