Skip to content

Instantly share code, notes, and snippets.

@faddah
Last active August 29, 2015 14:26
Show Gist options
  • Select an option

  • Save faddah/aefefa04284e24e68a41 to your computer and use it in GitHub Desktop.

Select an option

Save faddah/aefefa04284e24e68a41 to your computer and use it in GitHub Desktop.
a triangle of asterisks in a div done by plain JavaScript
<!DOCTYPE html>
<html>
<head>
<title>
The Triangle of Asterisks!
</title>
</head>
<body>
<h1>The Triangle of Asterisks!</h1>
<div id="asterisks">
</div>
<script src="triangle-loop.js"></script>
</body>
</html>
/*
var asterisk = '*';
var output = "";
var div = document.getElementById('asterisks');
var newtext;
var p = document.createElement("p");
for(var i = 0; i < 10; i++) {
output += asterisk;
newtext = document.createTextNode(output);
div.appendChild(p).appendChild(newtext);
console.log(output + '\n');
}
var asterisk = '*';
var output = "";
var div = document.getElementById('asterisks');
for(var i = 0; i < 10; i++) {
output += asterisk;
var p = document.createElement("p");
var newtext = document.createTextNode(output);
div.appendChild(p).appendChild(newtext);
console.log(output + '\n');
}
*/
var asterisk = '*';
var output = "";
var div = document.getElementById('asterisks');
for(var i = 0; i < 10; i++) {
output += asterisk;
var br = document.createElement("br");
var newtext = document.createTextNode(output);
div.appendChild(newtext.appendChild(br));
console.log(output + '\n');
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment