Skip to content

Instantly share code, notes, and snippets.

@wismer
Created April 24, 2017 14:55
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save wismer/b8e9533c1dddc8eab4f1de83dcccd1ae to your computer and use it in GitHub Desktop.
Save wismer/b8e9533c1dddc8eab4f1de83dcccd1ae to your computer and use it in GitHub Desktop.
Exported from Popcode. Click to import: https://popcode.org/?gist=b8e9533c1dddc8eab4f1de83dcccd1ae
<!DOCTYPE html>
<html>
<head>
<title>For Loop do now</title>
</head>
<body>
<button>click here</button>
</body>
</html>
{"enabledLibraries":["jquery"]}
//button click handler
// the slow way
var n = 0;
// I start out with a number that gets displayed in the HTML
$('body').append("<p>" + n + "</p>");
// and everytime I do that, I increase "n" by 1
n = n + 1;
$('body').append("<p>" + n + "</p>");
// But this way
n = n + 1;
$('body').append("<p>" + n + "</p>");
// is really, really repetitive and annoying
n = n + 1;
$('body').append("<p>" + n + "</p>");
// but there is a faster way.
$("button").click(function(){
//for loop declaration
for(var count = 0; count < 6; count = count + 1){
//prints hello to the page body
$("body").append("<p>" + count + "</p>");
}
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment