Practice Set - Arrays and Loops

by Angela Baruth

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

//This is the array I keep my friends in. (They don't seem to mind!)
var myboyfriends = [" Mario"," Daniel"," Jan"," Ian"," Marc"];

// Insert your code for #2 here

//Let's loop over each friend and say their name!
// start the first Array object and incresae the Array object and print out all Array object separat
for (var i=0; i<myboyfriends.length; i++) {
    console.log("Here a list of all my boy friends" + myboyfriends[i] + ".");
}

// Insert your code for #3 here

//Let's add my friend Rachel to this list at the very beginning, but without removing any of the old friends, because I still like them, too. Then I'll name them all again.
//Array.push () adds one or more elements to the end of the array and returns the new length of the array
myboyfriends.push ("Marc S.");
console.log(myboyfriends);

// Or
//Javascript pop () removes the last element from the array and returns that element.
 /* 
myboyfriends.pop("Marc S.");
console.log(myboyfriends);*/

// Or
//array.shift() entfernt das erste Element des Arrays und läßt die folgenden Elemente um einen Index nach vorn rutschen – die Elemente des Arrays werden nach links verschoben und das erste Element fällt dabei aus dem Array heraus.
 /* 
myboyfriends.shift("Marc S.");
console.log(myboyfriends);*/

// Insert your code for #4 here

//Loop over every integer up to 100, checking to see if its remainder is zero when divided by two. Skip the integers that do have a zero remainder (they're even), print the ones that don't to the console (they're odd).
// variable start with z+1 and should end by 100 (z<=100) means z greater equal 100 and increasing with 1   (x += y)
    
  /*  for (var z = 1; z < 100; z += 1) {
    console.log(z); }*/
    
// Or

// variable start with z+1 and should end by 100 (z<=100) means z greater equal 100 and increasing with 1 and give each second number out (x /= y)

      for (var z=1; z<=100; z++) {
    if (z%2 == 1) {
 console.log(z);
    }
};