Practice Set - Arrays and Loops
by Chetak Patel
HTML
<div>
<h3>Arrays & 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>
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>
Using the Array you made in the previous steps, iterate over its elements and write each one to the console.
</li>
<li>
Loop through the integers from 1 to 100 and write each to the console only if it is odd.
</li>
</ol>
</div>
<p>Output will appear below:</p>
<div id="output"></div>
JavaScript
"use strict";
// Insert your code for #1 here
var myFavoriteFruits = ["apples","grapes","kiwi","oranges"];
// Insert your code for #2 here
var bigString = myFavoriteFruits.join("<br>");
console.log(bigString);
logMessage(bigString + " is a " + typeof bigString);
// Insert your code for #3 here
var newFruitsArray = bigString.split("<br>");
console.log(newFruitsArray);
logMessage(newFruitsArray + " is a " + typeof newFruitsArray) ;
logMessage("newFruitsArray is an " + newFruitsArray.constructor.name);
// Insert your code for #4 here
function logMessage(msg, id){
if (!id){
id="output";
}
document.getElementById(id).innerHTML += msg + "<br>";
}