Exercise - Control flow

by Jennifer Piccione

JavaScript

/**
 * Exercise:
 *
 * Get to know your control flow and iteration
 *
 * Work in the console, or use "console.log()" to output to the console from the script
 *
 * these browser based functions can be used
 *     alert("Message string"); // will alert a message to the user
 *     var val = prompt("Question string?"); // will prompt the user for a value and return the value
 *     confirm("Message string"); // will ask the user to confirm with an ok, or cancel, returning a boolean
 */

// 1)
// Ask the user for a number
// Tell (alert) the user if it is even or odd.
// (hint: prompt() will always return a string)

var userNumString = prompt("Please give a number"); //returns string
var userNum = parseInt(userNumString); //converts string to number
if (userNum % 2 == 0) { 
    alert("Your number is even");
} else {
    alert("Your number is odd");
}

// 2)
// Ask the user how old they are
// If they are 70 or older, tell them they look great.
// If they are 18 or younger, tell them they should be in school.
// If they are between those ages, tell them to get a job.

var userAgeString = prompt("How old are you?"); //returns string
var userAge = parseInt(userAgeString); //converts string to number
if (userAge >= 70) {
    alert("You look great!");
} else if (userAge <= 18) {
    alert("You should be in school!");
} else {
    alert("Get a job!");
}

// 3) 
// We have an array of values
// Use a while() statement to go through each value of the array
// and add 1 to each number (but not strings)
// log the resulting array to the console

console.clear();
var myArray = [1,2,3,"Interlocutor", 4,5,6];
var i = 0;
while (i<7) {
    if (typeof(myArray[i]) == "number") {
        myArray[i] = myArray[i] + 1;
    }
    i++;
}
console.log(myArray);

// 4)
// Make an array of people's names
// Use a for statement to go through each and combine
// them into a single string separated by commas
// Log the result to the console.
// ex result: "John, Sally, Nick"

var pplArray =...