If Statement Olympics
The exercise will get progressively harder. You should at least strive to get one medal, but please try to go the extra mile!
Also, feel free to practice using ternary statements for extra credit too!
Preliminaries
- Write an
if
statement that prints "is greater than" if5
is greater than3
- Write an
if
statement that prints "is the length" if the length of"cat"
is3
- Write an
if/else
statement that checks if"cat"
is equal to"dog"
and prints, "not the same" when they are not equal.
Bronze Medal
- Using the below object, write an
if
statement that prints<theNameOfThePersonInObject> is allowed to go to the movie
if they are old enough (18 or older), and the opposite if they are not older than 18.
var person = {
name: "Bobby",
age: 12
}
- Using that same object, only allow them into the movie if their name starts with
"B"
- Using that same object, only allow them into the movie if their name starts with
"B"
and they are older than 18.
Silver Medal
- Write an if/else if/else statement that prints "strict" if
1
strictly equals"1"
, prints "loose" or "abstract" if1 == "1"
, and prints "not equal at all" if it doesn't print the other stuff. - Write an if statement that prints "yes" if
1
is less than or equal to2
AND (&&)2
is equal to4
Gold Medal
Gold may take some googling!
- Write an if statement that checks to see if
"dog"
is a string - Write an if statement that checks to see if
"true"
is a boolean - Write an if statement that checks to see if a variable has been defined or not
- Write an if statement asking if
"s"
is greater than12
?
Try it with a few more letters and a few numbers.
- Write a ternary statement that
console.log
s whether a number is odd or even. For example:
var myNum = 10;
// Write your ternary here to log if `myNum` is odd or even.
(It should continue to work correctly even if myNum
changes to a different number).