Computer Science Principles
Course Progress
0/0
Objectives in LxD
3.4 Strings
3.4 Strings
3.3 Mathematical Expressions
3.3 Math Expressions
3.2 Data Abstractions
3.2 Data Abstractions
3.1 Variables & Assignments
3.1 Variables and Assignments
3.1 Variables and Assignments (Sample)
Intro to Python
Intro to Javascript
Variables and HTML DOM (Sample)
3.5 Boolean Expressions (PY)
3.5 Boolean Expressions (JS)
3.8 Iterations
3.7 Nested Conditionals
3.6 Conditionals
3.8 Iterations
3.7 Nested Conditionals
3.6 Conditionals
3.13 Developing Procedures
3.12 Calling Procedures
3.10 Lists
3.13 Developing Procedures
3.10 Lists
3.9 Developing Algorithms
3.17 Algorithmic Efficiency
3.9 Algorithms
3.17 Algorithmic Efficiency
3.15 Random Numbers (pseudocode)
3.15 Random Numbers (js)
3.15 Random Numbers (py)
BI 3 Review
Sprint View
Week 5
3.5 Boolean Expressions (JS)
3.5 Boolean Expressions (JS)
1 min read
🔑 Boolean Hacks in Java: Whack-A-Mole
This notebook has short Boolean challenges. Edit the code where it says TODO to make it work.
Challenge 1: Mole Hit
Set hit to true if the player hits the mole. Print “Hit!” if hit is true, otherwise print “Miss!”.
%%js
let hit = false; // Change to true if you hit the mole
// TODO: check if hit is true
if (hit) {
console.log("Hit!");
} else {
console.log("Miss!");
}
<IPython.core.display.Javascript object>
Challenge 2: Out of Lives
Each time the player misses a mole, they lose a life. If lives reach 0, print “Game Over!”.
%%js
let lives = 3; // Decrease when a mole is missed
let missed = true; // Change to true if player misses a mole
// TODO: decrease lives when a mole is missed
if (missed) {
lives -= 1;
}
if (lives === 0) {
console.log("Game Over!");
} else {
console.log("Keep playing! Lives left: " + lives);
}
Challenge 3: High Score Boost
If the player’s score reaches 10 or more, give them a bonus of +5 points.
%%js
let score = 8; // Increase when a mole is hit
// TODO: add 5 bonus points when score is 10 or higher
if (score >= 10) {
score += 5;
console.log("New High Score! Bonus awarded.");
} else {
console.log("Try again!");
}
console.log("Final score: " + score);
Challenge 4: Bonus Mole Condition
A golden mole should only give a bonus if both goldenMole and hit are true. Fix the condition.
%%js
let goldenMole = true; // Special mole appears
let hit = true; // Change to true if the mole is hit
// TODO: fix the condition so bonus only happens when BOTH are true
if (goldenMole || hit) { // <-- wrong condition
console.log("Bonus!");
} else {
console.log("No bonus.");
}