Practice Set - Arrays and Loops

by Ramya Ranganathan

HTML

<h3>Arrays &amp; Looping</h3>
<h4>4 Tasks for this Practice Set:</h4>
<ol>
    <li>
        Make an Array of strings and assign it to a variable. You may make the strings a list of anything you like and use a variable name of your choice.</li><li>
    Using the Array you made in the previous step, iterate over its elements and write each one to the console.
    </li>
    <li>
        Use one of the methods of the Array object to insert a new element to the beginning of your array. Looking through the methods on the Array reference page at <a href="http://www.w3schools.com/jsref/">W3Schools</a> may be helpful.    
    </li>
    <li>
        Loop through the integers from 1 to 100 and write each to the console only if it is odd.
    </li>
</ol>

JavaScript

"use strict"; 

// Insert your code for #1 

var Mygrocerylist = ["Milk","Yogurt","Cheese","Crackers","Juice","Cereals","Sugar"];
console.log("Mygrocerylist[0] :" + Mygrocerylist[0]);



// Insert your code for #2 here
var index, len;
//var Mygrocerylist = ["milk","yogurt","cheese","crackers","juice","cereals","sugar"];
for (index = 0, len = Mygrocerylist.length; index < len; ++index) {
    console.log(Mygrocerylist[index]);
}


// Insert your code for #3 here
var Mygrocerylist = ["milk","yogurt","cheese","crackers","juice","cereals","sugar"];
Mygrocerylist.unshift("salt");
for (index = 0, len = Mygrocerylist.length; index < len; ++index) {
    console.log(Mygrocerylist[index]);
}
//console.log("New First Element of Array : " + Mygrocerylist[0]);


// Insert your code for #4 here
var i, int_len;
for (i = 1, int_len = 100; i < int_len; ++i) {
    if ( i % 2 != 0 ) {
        console.log(i);
    }
}