Boolean Popcorn Hacks
Popcorn Hacks
Popcorn Hacks
Exercise 1
Create a variable that holds a true or false value. Print a message that says “This is true!” if the value is true and “This is false!” if the value is false.
%%js
let isSunny = true; // Change this to false to test the other case
if (isSunny) {
// Your code here
} else {
// Your code here
}
Exercise 2
Write a program that checks if both conditions are true: the weather is nice (true) and it’s a weekend (true). Print “Go outside!” if both are true, and “Stay inside!” otherwise.
%%js
let isWeekend = true; // Change to false to test the other case
let isNiceWeather = true; // Change to false to test the other case
if (/* Your code here */) {
// Your code here
} else {
// Your code here
}
Exercise 3
Write a program that prints “Time to go to the beach!” if it’s either sunny or the weekend. Use the OR ( | ) operator. |
%%js
let isSunny = true; // Change to false to test the other case
let isWeekend = false; // Change to true to test the other case
if (/* Your code here */) {
// Your code here
} else {
// Your code here
}
Exercise 4
Write a program that prints “Not sunny today” if isSunny is false, and “It’s sunny!” if isSunny is true. Use the NOT (!) operator to invert the value of isSunny.
%%js
let isSunny = false; // Change to true to test the other case
if (/* Your code here */) {
// Your code here
} else {
// Your code here
}
Exercise 5
Create a program that checks if a user is both logged in (true) and has admin rights (true). Print “Access granted!” if both are true, and “Access denied!” if either is false.
%%js
let isLoggedIn = true; // Change to false to test the other case
let isAdmin = true; // Change to false to test the other case
if (/* Your code here */) {
// Your code here
} else {
// Your code here
}
Exercise 6
Use a ternary operator to decide whether a user is allowed access based on their age. If the age is 18 or above, print “You are allowed access.” If below 18, print “Sorry, you are too young.”
%%js
let age = 16; // Change this value to test different cases
let accessMessage = /* Your code here */;
console.log(accessMessage); // Will print based on age
Exercise 7
Write a program that checks if a user is both a VIP (true) and has a VIP ticket (true). If both conditions are true, print “VIP Access granted!” Otherwise, print “Access denied!”
%%js
let isVIP = true; // Change to false to test the other case
let hasVIPticket = false; // Change to true to test the other case
if (/* Your code here */) {
// Your code here
} else {
// Your code here
}