<html>
<head>
<script>
// Example of a Script in the head section
function showPlaces()
{
// Create and array of places
var PlaceArray = new Array("a pie","the sky","a cup","the truck");
// var Place = PlaceArray[interatingCounter]: this line for discussion purposes
// PlaceArray.length is a property. No processing is done to determine its value
for (var i = 0; i < PlaceArray.length; i = i+1)
{
var Place = PlaceArray[i]
document.write(Place);
document.write("<br />") // breaking new line
}
}
</script>
</head>
<body>
<h1>Introduction</h1>
Examples of interation with JavaScript. These are for discussion purposes and do NOT illustrate best practices. Instead we are using document.write() as an easy way to display outcomes, and executing JavaScript inline with the HTML.
<h1>JavaScript Iterating Over Arrays</h1>
<h2>Display Fruit in an array.</h2>
<script>
// Create an array of Fruit
var FruitArray= new Array("Apples","Peaches","Watermelons","Pomegranates");
for (x in FruitArray)
{
document.write(FruitArray[x]);
document.write("<br />"); // breaking new line
}
</script>
<h2>Display places you might find fruit.</h2>
<script>
// this time use a function in the head section of the html page
showPlaces()
</script>
<h2>Fruit in various places</h2>
<script>
// Create the arrays of Fruit and Toppings
var FruitArray= new Array("Apples","Peaches","Watermelons","Pomegranates");
var PlaceArray = new Array("a pie","the sky","a cup","the truck");
var Counter = 0 ;
//var Place = PlaceArray[Counter] : this line for discussion purposes
// Looping through fruit with toppings
for (x in FruitArray)
{
Place = PlaceArray[Counter]
document.write("I like " + FruitArray[x] + " in " + Place);
document.write("<br />"); // breaking new line
Counter +=1; // increment the counter for the next destination
}
</script>
<h2>Fruit in various places done a different way.</h2>
<script>
// function to allow insertion of parameters in a string
String.prototype.format = function()
{
var s = this;
for (var i = 0; i < arguments.length; i++)
{
var reg = new RegExp("\\{" + i + "\\}", "gm");
s = s.replace(reg, arguments[i]);
}
return s;
}
// function to display the fruit (uses .format above)
function showFruit(element)
{
place = PlaceArray[Counter];
document.write(("I like {0} in {1} <br />").format(element,place));
// line above replaces: document.write("I like " + element + " in " + place)
// this is no longer required: document.write("<br />"); // breaking new line
Counter +=1; // increment the counter for the next destination
}
// Create the arrays of Fruit and Toppings
var FruitArray= new Array("Apples","Peaches","Watermelons","Pomegranates");
var PlaceArray = new Array("a pie","the sky","a cup","the truck");
var Counter = 0 ;
var Place = PlaceArray[Counter] // this line for discussion purposes
// use forEach method to iterate over the array of Fruit
// calling the showFruit function
FruitArray.forEach(showFruit);
/* line above replaces:
{
Place = PlaceArray[Counter]
document.write("I like " + FruitItem + " in " + Place)
document.write("<br />"); // breaking new line
Counter +=1 // increment the counter for the next destination
}
*/
</script>
</body>
</html>
Output
This bin was created anonymously and its free preview time has expired (learn why). — Get a free unrestricted account
Dismiss xKeyboard Shortcuts
Shortcut | Action |
---|---|
ctrl + [num] | Toggle nth panel |
ctrl + 0 | Close focused panel |
ctrl + enter | Re-render output. If console visible: run JS in console |
Ctrl + l | Clear the console |
ctrl + / | Toggle comment on selected lines |
ctrl + ] | Indents selected lines |
ctrl + [ | Unindents selected lines |
tab | Code complete & Emmet expand |
ctrl + shift + L | Beautify code in active panel |
ctrl + s | Save & lock current Bin from further changes |
ctrl + shift + s | Open the share options |
ctrl + y | Archive Bin |
Complete list of JS Bin shortcuts |
JS Bin URLs
URL | Action |
---|---|
/ | Show the full rendered output. This content will update in real time as it's updated from the /edit url. |
/edit | Edit the current bin |
/watch | Follow a Code Casting session |
/embed | Create an embeddable version of the bin |
/latest | Load the very latest bin (/latest goes in place of the revision) |
/[username]/last | View the last edited bin for this user |
/[username]/last/edit | Edit the last edited bin for this user |
/[username]/last/watch | Follow the Code Casting session for the latest bin for this user |
/quiet | Remove analytics and edit button from rendered output |
.js | Load only the JavaScript for a bin |
.css | Load only the CSS for a bin |
Except for username prefixed urls, the url may start with http://jsbin.com/abc and the url fragments can be added to the url to view it differently. |