Skip to the content.

Booleans Popcorn Hacks

My finished boolean 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) {
    console.log("This is true! :D");//prints that isSunny is true 
} else {
    console.log("This is false...")//prints that isSunny is false
}
<IPython.core.display.Javascript object>

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 (isWeekend && isNiceWeather) {
    console.log("Go outside!");// prints to go outside
} else {
    console.log("Stay inside!");//tells to stay inside
}

<IPython.core.display.Javascript object>

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 = true;  // Change to true to test the other case

if (isSunny || isWeekend) {
    console.log("Time to go to the beach!");//prints to go to the beach
} else {
    console.log("Don't go to the beach... :("); //tells you not to go to the beach
}

<IPython.core.display.Javascript object>

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 (!isSunny) {
    console.log("Not sunny today");//prints that it's not sunny
} else {
    console.log("It's sunny!");//prints that it's sunny
}
<IPython.core.display.Javascript object>

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 (isLoggedIn && isAdmin) {
    console.log("Access granted!");//prints that access is granted
} else {
    console.log("Access denied!")//prints that access is denied
}
<IPython.core.display.Javascript object>

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 = age >= 18 ? true == "You are allowed access." : "Sorry, you are too young.";//checks if they are above 18 years old

console.log(accessMessage);  // Will print based on age

<IPython.core.display.Javascript object>

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 (isVIP && hasVIPticket) {
    console.log("VIP Access granted!");//grants access
} else {
    console.log("Access denied!");//prints that access is denied
}
<IPython.core.display.Javascript object>