AllThingsNetwork.org Logo

Security Simplified



Loops, Conditional Statements, Functions, Variables, Parameters, Arrays, Associative Arrays

Video

Presentation Link


Example #1

FOR Loop

var scores = [24, 32, 17];
var arrayLength = scores.length;
var roundNumber = 0;
var msg = '';

for (var i = 0; i < arrayLength; i++) {

	roundNumber = (i + 1);

	msg += 'Round ' + roundNumber + ': ';

	msg += scores[i] + '
'; } document.getElementById('answer').innerHTML = msg;

WHILE Loop

var i = 1;
var msg = '';

while (i < 10) {
	msg += i + ' x 5 = ' + (i * 5) + '
'; i++; } document.getElementById('answer').innerHTML = msg;

DO...WHILE Loop

var i = 1;
var msg = '';

do {
	msg += i + ' x 5 = ' + (i * 5) + '
'; i++; } while (i < 1); document.getElementById('answer').innerHTML = msg;

Function

var score = 75;
var msg = '';

function congratulate() {
	msg += 'Congratulations! ';
}

if (score >= 50) {
	congratulate();
	msg += 'Proceed to the next round.';
}

var el = document.getElementById('answer');
el.innerHTML = msg;

Arrays

var colors1;
colors1 = ['white', 'black', 'custom'];
var colors2 = new Array('white', 'black', 'custom');


var el1 = document.getElementById('colors1');
el1.textContent = colors1[0];
var el2 = document.getElementById('colors2');
el2.textContent = colors2[0];

Associative Arrays

var myObject = {};
myObject.city = "McKinney";
myObject.state = "Texas";
myObject.country = "United States";
console.log(myObject.length);
console.log("City is: " + myObject['city']);
console.log("City is: " + myObject.city);

Example #2

This example focuses on the FOREACH loop. It will continue iterating through every item in a list of items until the end.

Contact Input






Contact List

FOREACH Loop Code Snippet

var count = 0;
contactList.forEach(function(rowData) {
    var row = document.createElement('tr');

    var nameCell = document.createElement('td');
    nameCell.appendChild(document.createTextNode(rowData.name));
    row.appendChild(nameCell);
    var phoneCell = document.createElement('td');
    phoneCell.appendChild(document.createTextNode(rowData.phone));
    row.appendChild(phoneCell);
    var addressCell = document.createElement('td');
    addressCell.appendChild(document.createTextNode(rowData.address));
    row.appendChild(addressCell);
    var emailCell = document.createElement('td');
    emailCell.appendChild(document.createTextNode(rowData.email));
    row.appendChild(emailCell);
    var actionCell = document.createElement('td');
    actionCell.innerHTML = "";
    row.appendChild(actionCell);
    newTableBody.appendChild(row);
    count++;
});