Exercise - Control flow
by patrickliang
JavaScript
/**
* Exercise:
*
* Get to know your control flow and iteration
*
* Complete the three (+1 bonus) exercises (below)
* using the following browser based functions:
*
* 1. alert()
* alert("Message string");
* Alert a message to the user
*
* 2. prompt()
* var val = prompt("Question string?");
* will prompt the user for a value and return the value
*
* 3. confirm()
* confirm("Message string");
* will ask the user to confirm with an ok, or cancel, returning a boolean
*
* Hint:
* Work in the console, or use "console.log()"
* to output to the console from the script
*/
/*alert("Buy these pants!");
var value = prompt("How much would you pay?");
console.log(value);
confirm("Are you sure you want to delete this?");*/
// 1)
// Ask the user for a number (using prompt())
// Tell the user if it is even or odd (using alert())
// (hint: prompt() will always return a string)
// (hint: the modulo operator, %, can help determine oddness or evenness)
// your solution to #1 here
/*var num = prompt("Give me a number.");
if (num % 2 == 0) {
alert("This number is even.");
}
else {
alert("This number is odd!");
}*/
// 2)
// Ask the user how old they are (using prompt())
// 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.
// your solution to #2 here
/*var age = prompt("How old are you?");
if (age >= 70) {
alert("You look great!");
}
else if (age <= 18) {
alert("You should be in school!");
}
else if (age > 18 && age < 70) {
alert("Go 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
var myArray = [1,2,3,"Interlocutor", 4,5,6];
// your solution to #3 here
var i = 0;
while (i < myArray.length) {
//var arrValue = myArray[i];
if (typeof myArray[i] == "number") {
...