Practice Set - Arrays and Loops

by Thomas Smalls

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>
    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>

JavaScript

"use strict";

// Insert your code for #1 here
var myClothes = ["socks", "shirts", "hats", "pants", "shoes"];
//console.log(myClothes.length);

// Insert your code for #2 here
myClothes.unshift("neckties");
//console.log(myClothes.length);

var showClothes = "";
myClothes.forEach(listClothes);

function listClothes(value, index) {
  showClothes = index + ". " + value;
  console.log(showClothes);
}

// Insert your code for #4 here
var lookingForOdd = 0;
while (lookingForOdd < 100) {
  lookingForOdd++;
  if (lookingForOdd % 2 != 0) {
    console.log(lookingForOdd + " is odd.");
    lookingForOdd++;
  } else if (lookingForOdd % 2 == 0) {
    lookingForOdd++;
  } else {
    console.log("All done!");
  }
}
confirm("Hope you had a great day! Press CTRL+SHIFT+I to see console.");