<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>
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 here
// Array of Names of pets I've had in my life
// Honestly, I came up with my pet names before I saw Larry's video of his favorite pets! lol
var myPets = ['Mittens', 'Midnight', 'Blue', 'Shadow', 'Sailor', 'Harry', 'Roxy', 'Bubbles', 'Ellie'];
// Insert your code for #2 here
// Iterate over all elements in myPets array
for (var i = 0; i < myPets.length; i++) {
// Print my pet names the console
console.log(myPets[i]);
}
// Insert your code for #3 here
// Insert a pet name at the beginning
myPets.unshift('Monkey');
console.log(myPets);
// Insert your code for #4 here
// Loop through integers 1 to 100
/*for (var i = 0; i < 100; i += 2) {
console.log(i +1);
}*/
// Or
// Initialize i to 1 then loop through 100 and print every other number
for (var i = 1; i < 100; i += 2) {
console.log(i);
}
Please Whitelist JSFiddle in your content blocker.
Help keep JSFiddle free for always by one of two ways:
Whitelist JSFiddle in your content blocker (two clicks)
Go PRO and get access to additional PRO features →
Join the 4+ million users, and keep the JSFiddle dream alive.
Ad-free
All ads in the editor and listing pages are turned completely off.
Use pre-released features
You get to try and use features (like the Palette Color Generator) months before everyone else.
Fiddle collections
Sort and categorize your Fiddles into multiple collections.
Private collections and fiddles
You can make as many Private Fiddles, and Private Collections as you wish!
Console
Debug your Fiddle with a minimal built-in JavaScript console.