Skip to content

Instantly share code, notes, and snippets.

Created August 29, 2016 00:41
Show Gist options
  • Save anonymous/4d8cb5c5bd118702524cfc32b330e30e to your computer and use it in GitHub Desktop.
Save anonymous/4d8cb5c5bd118702524cfc32b330e30e to your computer and use it in GitHub Desktop.
JS Bin // source http://jsbin.com/winuje
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width">
<title>JS Bin</title>
</head>
<body>
<script id="jsbin-javascript">
//Control Flow
//Control flow is exactly what it sounds likes--exercising control over
//the flow of one's program, and there are many tactics toward this end. Two conditional
//tactics are outlined below, if statements and switch statements.
//The if-else and if statements incorporate block statements with different conditions
//that, if met or not met, determine how the program moves forward. See below.
var brendansBoss = function(trait, goodnessLevel) {
if (trait === "asshole" && goodnessLevel <= 5) {
console.log("I quit")
}
else if (trait === "jovial human" && goodnessLevel > 5) {
console.log("I shall stay. And the beer shall flow.")
}
else {
console.log("What fresh hell is this")
}
}
brendansBoss("jovial human", 8);
//The above code is a function including an if statement that will log a different text
//based on what the user inputs. The flow is controlled, in this case, by the user, although
//in this scenario it's more simply the text that is controlled by them--but if statements can
// include functions, and more if statements inside of them, and it can all become a deeply
//layered hierarchy of choices--thus, in such a case, the user's, or programmer's, input,
//determines if the program shuts down, or gives a single output, or moves into performing
//another action.
//Switch statements are another way of doing this. Here's one below.
var mood = "fool";
switch(mood) {
case "asshole":
console.log("Today you are beinga huge " + mood);
break;
case "nice guy":
console.log("Thanks for being such a " + mood);
break;
case "tyrant":
console.log("You slaughtered that entire village...what the hell, you're just a " + mood);
break;
case "fool":
console.log("I can't believe they pay you to manage me when you're such a " + mood);
break;
}
//When to use one or the other is largely a matter of functionality. If statements are best
//if there are just a few options or fewer, and especially if there are just two options.
// In such a case, an if-else is a very simple answer. If this happens, do this...but if
//instead this happens, then do this. Option 1 or option 2, and that's it. An if statement
//is also useful as an even simpler conditional--if this condition is true, do this. If
//declared with no else, then the program will simply either DO the thing, or just move on
//without doing anything.
//On the other hand, if you're going to have a larger number of options, a switch statement
//can prove very useful.
//If you think of the fact that a computer's software is all, entirely based upon binary--
//a series of 0s and 1s, then you can see how a conditional statement is a macrocosmic
//version of the same. It can be a complex series of Yes or No responses that lead a program
//forward or stop a program or change it completely based on the response.
//A choose your own adventure is a perfect analogy for control flow--do you remember
//reading one and then, all of a sudden, two choices in, the story's over? Well, that's
//the power of a conditional statement for control flow. You can open up an entire universe,
// or close one down.
//Below is an example of a choose your own adventure game using switch statements
//to determine the forward movement of the program.
var user = prompt('You start at a new job. Everything is going well until one day, your boss enters your office and says "Now it is time for us to have sex." Do you ACCEPT, REJECT, or ATTACK?').toUpperCase();
switch(user) {
case 'ACCEPT':
var pervert = prompt('You dirty bird. Your boss now gives you a promotion; he/she offers for you to be his/her boss, and make twice his/her salary. Do you Accept?').toUpperCase();
if ((pervert = 'YES').toUpperCase()) {
console.log("You are a soulless, corrupt, immoral person. You are sent to corporate prison, where they feed you delicious food and give you money and you're released early because in fact life isn't fair so you end up doing fine.");
}
else if ((pervert = 'NO').toUpperCase()) {
console.log("You are an ethically upright individual, you naive son of a bitch. You are fired on the spot and can't find another job for ten years, so you live in poverty.");
}
else {
console.log("Tight-lipped, myeh? Away with you!");
}
break;
case 'REJECT':
console.log('Really? Opportunity of a lifetime? Your boss is hot. Just kidding, you made the right choice.')
break;
case 'ATTACK':
var battle = prompt('The fires of helmsdeep shall be lit...once more. Do you attack with a WORG, a POLYMER RESIN, or WIT AND CUNNING?').toUpperCase();
if (battle === ('WORG' || 'POLYMER RESIN')){
console.log("Violence is never the answer!!!! Even with an ingenious polymer resin! The police are called and you're escorted off premises and you hang your head in shame.")
}
else if ('worg' && 'polymer resin'){
console.log("damn calm down sir")
}
else {
console.log("You have not only nobly, permanently debased this horrible, harrassing person, but your lawyer and yourself sue him/her for millions and bankrupt the company, which was run by thieves, and you live happier ever after.")
}
break;
default:
console.log("You can't escape life's many travails!!!")
}
</script>
<script id="jsbin-source-javascript" type="text/javascript">//Control Flow
//Control flow is exactly what it sounds likes--exercising control over
//the flow of one's program, and there are many tactics toward this end. Two conditional
//tactics are outlined below, if statements and switch statements.
//The if-else and if statements incorporate block statements with different conditions
//that, if met or not met, determine how the program moves forward. See below.
var brendansBoss = function(trait, goodnessLevel) {
if (trait === "asshole" && goodnessLevel <= 5) {
console.log("I quit")
}
else if (trait === "jovial human" && goodnessLevel > 5) {
console.log("I shall stay. And the beer shall flow.")
}
else {
console.log("What fresh hell is this")
}
}
brendansBoss("jovial human", 8);
//The above code is a function including an if statement that will log a different text
//based on what the user inputs. The flow is controlled, in this case, by the user, although
//in this scenario it's more simply the text that is controlled by them--but if statements can
// include functions, and more if statements inside of them, and it can all become a deeply
//layered hierarchy of choices--thus, in such a case, the user's, or programmer's, input,
//determines if the program shuts down, or gives a single output, or moves into performing
//another action.
//Switch statements are another way of doing this. Here's one below.
var mood = "fool";
switch(mood) {
case "asshole":
console.log("Today you are beinga huge " + mood);
break;
case "nice guy":
console.log("Thanks for being such a " + mood);
break;
case "tyrant":
console.log("You slaughtered that entire village...what the hell, you're just a " + mood);
break;
case "fool":
console.log("I can't believe they pay you to manage me when you're such a " + mood);
break;
}
//When to use one or the other is largely a matter of functionality. If statements are best
//if there are just a few options or fewer, and especially if there are just two options.
// In such a case, an if-else is a very simple answer. If this happens, do this...but if
//instead this happens, then do this. Option 1 or option 2, and that's it. An if statement
//is also useful as an even simpler conditional--if this condition is true, do this. If
//declared with no else, then the program will simply either DO the thing, or just move on
//without doing anything.
//On the other hand, if you're going to have a larger number of options, a switch statement
//can prove very useful.
//If you think of the fact that a computer's software is all, entirely based upon binary--
//a series of 0s and 1s, then you can see how a conditional statement is a macrocosmic
//version of the same. It can be a complex series of Yes or No responses that lead a program
//forward or stop a program or change it completely based on the response.
//A choose your own adventure is a perfect analogy for control flow--do you remember
//reading one and then, all of a sudden, two choices in, the story's over? Well, that's
//the power of a conditional statement for control flow. You can open up an entire universe,
// or close one down.
//Below is an example of a choose your own adventure game using switch statements
//to determine the forward movement of the program.
var user = prompt('You start at a new job. Everything is going well until one day, your boss enters your office and says "Now it is time for us to have sex." Do you ACCEPT, REJECT, or ATTACK?').toUpperCase();
switch(user) {
case 'ACCEPT':
var pervert = prompt('You dirty bird. Your boss now gives you a promotion; he/she offers for you to be his/her boss, and make twice his/her salary. Do you Accept?').toUpperCase();
if ((pervert = 'YES').toUpperCase()) {
console.log("You are a soulless, corrupt, immoral person. You are sent to corporate prison, where they feed you delicious food and give you money and you're released early because in fact life isn't fair so you end up doing fine.");
}
else if ((pervert = 'NO').toUpperCase()) {
console.log("You are an ethically upright individual, you naive son of a bitch. You are fired on the spot and can't find another job for ten years, so you live in poverty.");
}
else {
console.log("Tight-lipped, myeh? Away with you!");
}
break;
case 'REJECT':
console.log('Really? Opportunity of a lifetime? Your boss is hot. Just kidding, you made the right choice.')
break;
case 'ATTACK':
var battle = prompt('The fires of helmsdeep shall be lit...once more. Do you attack with a WORG, a POLYMER RESIN, or WIT AND CUNNING?').toUpperCase();
if (battle === ('WORG' || 'POLYMER RESIN')){
console.log("Violence is never the answer!!!! Even with an ingenious polymer resin! The police are called and you're escorted off premises and you hang your head in shame.")
}
else if ('worg' && 'polymer resin'){
console.log("damn calm down sir")
}
else {
console.log("You have not only nobly, permanently debased this horrible, harrassing person, but your lawyer and yourself sue him/her for millions and bankrupt the company, which was run by thieves, and you live happier ever after.")
}
break;
default:
console.log("You can't escape life's many travails!!!")
}
</script></body>
</html>
//Control Flow
//Control flow is exactly what it sounds likes--exercising control over
//the flow of one's program, and there are many tactics toward this end. Two conditional
//tactics are outlined below, if statements and switch statements.
//The if-else and if statements incorporate block statements with different conditions
//that, if met or not met, determine how the program moves forward. See below.
var brendansBoss = function(trait, goodnessLevel) {
if (trait === "asshole" && goodnessLevel <= 5) {
console.log("I quit")
}
else if (trait === "jovial human" && goodnessLevel > 5) {
console.log("I shall stay. And the beer shall flow.")
}
else {
console.log("What fresh hell is this")
}
}
brendansBoss("jovial human", 8);
//The above code is a function including an if statement that will log a different text
//based on what the user inputs. The flow is controlled, in this case, by the user, although
//in this scenario it's more simply the text that is controlled by them--but if statements can
// include functions, and more if statements inside of them, and it can all become a deeply
//layered hierarchy of choices--thus, in such a case, the user's, or programmer's, input,
//determines if the program shuts down, or gives a single output, or moves into performing
//another action.
//Switch statements are another way of doing this. Here's one below.
var mood = "fool";
switch(mood) {
case "asshole":
console.log("Today you are beinga huge " + mood);
break;
case "nice guy":
console.log("Thanks for being such a " + mood);
break;
case "tyrant":
console.log("You slaughtered that entire village...what the hell, you're just a " + mood);
break;
case "fool":
console.log("I can't believe they pay you to manage me when you're such a " + mood);
break;
}
//When to use one or the other is largely a matter of functionality. If statements are best
//if there are just a few options or fewer, and especially if there are just two options.
// In such a case, an if-else is a very simple answer. If this happens, do this...but if
//instead this happens, then do this. Option 1 or option 2, and that's it. An if statement
//is also useful as an even simpler conditional--if this condition is true, do this. If
//declared with no else, then the program will simply either DO the thing, or just move on
//without doing anything.
//On the other hand, if you're going to have a larger number of options, a switch statement
//can prove very useful.
//If you think of the fact that a computer's software is all, entirely based upon binary--
//a series of 0s and 1s, then you can see how a conditional statement is a macrocosmic
//version of the same. It can be a complex series of Yes or No responses that lead a program
//forward or stop a program or change it completely based on the response.
//A choose your own adventure is a perfect analogy for control flow--do you remember
//reading one and then, all of a sudden, two choices in, the story's over? Well, that's
//the power of a conditional statement for control flow. You can open up an entire universe,
// or close one down.
//Below is an example of a choose your own adventure game using switch statements
//to determine the forward movement of the program.
var user = prompt('You start at a new job. Everything is going well until one day, your boss enters your office and says "Now it is time for us to have sex." Do you ACCEPT, REJECT, or ATTACK?').toUpperCase();
switch(user) {
case 'ACCEPT':
var pervert = prompt('You dirty bird. Your boss now gives you a promotion; he/she offers for you to be his/her boss, and make twice his/her salary. Do you Accept?').toUpperCase();
if ((pervert = 'YES').toUpperCase()) {
console.log("You are a soulless, corrupt, immoral person. You are sent to corporate prison, where they feed you delicious food and give you money and you're released early because in fact life isn't fair so you end up doing fine.");
}
else if ((pervert = 'NO').toUpperCase()) {
console.log("You are an ethically upright individual, you naive son of a bitch. You are fired on the spot and can't find another job for ten years, so you live in poverty.");
}
else {
console.log("Tight-lipped, myeh? Away with you!");
}
break;
case 'REJECT':
console.log('Really? Opportunity of a lifetime? Your boss is hot. Just kidding, you made the right choice.')
break;
case 'ATTACK':
var battle = prompt('The fires of helmsdeep shall be lit...once more. Do you attack with a WORG, a POLYMER RESIN, or WIT AND CUNNING?').toUpperCase();
if (battle === ('WORG' || 'POLYMER RESIN')){
console.log("Violence is never the answer!!!! Even with an ingenious polymer resin! The police are called and you're escorted off premises and you hang your head in shame.")
}
else if ('worg' && 'polymer resin'){
console.log("damn calm down sir")
}
else {
console.log("You have not only nobly, permanently debased this horrible, harrassing person, but your lawyer and yourself sue him/her for millions and bankrupt the company, which was run by thieves, and you live happier ever after.")
}
break;
default:
console.log("You can't escape life's many travails!!!")
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment