Exercise – Operators & Control Structures
by manu troiani
JavaScript
/**
* Exercise:
*
* Get to know your control structures
*
* 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 (using prompt())
/*function isEven( n ){
return ( n % 2 == 0 : ( alert"even" ) : ( alert "odd") );
}*/
function isEven( n ){
var isEven = true;
if ( n % 2 !== 0 )
isEven = false;
return isEven;
}
function IsEvenOrOdd (n){
var out;
isEven(n) ? (out = "is even" ): (out= "is odd");
return out;
}
var userInput = prompt("give me a number and I'll tell you wether is even or odd");
//console.log(isEven(7));
console.log(userInput);
//alert( isEven(userInput));
alert( "I am a magician, I know that your number is "+ IsEvenOrOdd(userInput) + "!!!" );
// Tell the user if it is even or odd (using alert())
// (hint: prompt() will always return a string)
// 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.
var userAge = prompt("how old are you?");
function calcultateYouth( age){
var out;
if (age <=18)
out = " you should be in school!";
if (age >=70)
out = " you still look great inside!"
if ( age>18 & age<70)
out = "if you do not have a job, you surely have plenty of time to look for one!"
return out;
}
alert( calcultateYouth(userAge) );
// 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...