AP Computer Science Principles Unit 4: Conditionals
Name _____________________________________ Period ___
Conditionals Notes #4
Objective: Write code using conditionals, given requirements.
EXAMPLES
Directions: Write code, given the requirements.
1. In a racing game, the variable bonusPoints represents the bonus points a player earns.
The bonus points depend on the player’s finishing position. The value of bonusPoints
should be 100 if position is 1 (the player finishes in first place) and 50 otherwise.
ANSWER
var bonusPoints;
if (position === 1) {
bonusPoints = 100;
} else {
bonusPoints = 50;
}
2. Given a variable min that stores the minimum value and a variable num, how can you
check if min should be updated?
var min = 3;
if (num < min)
min = num;
1
AP Computer Science Principles Unit 4: Conditionals
3.. The cost of parking in a garage is based on the number of hours a car is parked.
● For the first 3 hours, the cost is $4 per hour.
● For any additional hours beyond the first 3, the cost is $2 per hour.
STRATEGY: Investigate a few cases. Look for the pattern.
Case: parked for 3 hours
Hour 1 2 3
cost 4 4 4
Total cost is 3 hours * $4 = $12
Case: parked for 9 hours
Hour 1 2 3 4 5 6 7 8 9
cost 4 4 4 2 2 2 2 2 2
Total cost is 3 hours * 4 + 6 hours * 2 = $24
Case: parked for 7 hours
Hour 1 2 3 4 5 6 7
cost 4 4 4 2 2 2 2
Total cost is 3 hours * 4 + 4 hours * 2 = $20
ANSWER
var numHours;
if (numHours <= 3)
cost = numHours * 4;
else
cost = 3 * 4 + (numHours - 3) * 2;
2
AP Computer Science Principles Unit 4: Conditionals
PRACTICE
Directions: Write code, given the requirements.
1. The cost of a gym membership is based on the number of months a customer
subscribes.
● For the first 6 months, the cost is $30 per month.
● For any additional months beyond the first 6, the cost is $20 per month.
ANSWER
var numMonths;
if (numMonths <= 6)
cost = numMonths * 30;
else
cost = 6 * 30 + (numMonths - 6) * 20;
2. In a fitness app, the variable calorieGoal represents the daily calorie goal a user
should aim for. The goal depends on the user’s activity level. The value of calorieGoal
should be 2500 if activityLevel is "high" and 2000 otherwise.
var calorieGoal;
if (activityLevel == "high")
calorieGoal = 2500;
else
calorieGoal = 2000;
3. Given a variable max that stores the maximum value and a variable num, how can you
check if max should be updated?
var max = 5; var num;
if (num > max)
max = num;
3
AP Computer Science Principles Unit 4: Conditionals
4. The cost of a customer’s water bill is based on the number of gallons of water the
customer uses.
● For the first 50 gallons of water, the cost is $2 per gallon.
● For any additional gallons, the cost is $3 per gallon.
ANSWER
var numGallons;
if (numGallons <= 50)
cost = numGallons * 2;
else
cost = 50 * 2 + (numGallons - 50) * 3;
5. In a weather app, the variable outfitRecommendation suggests what a user should wear
based on the temperature. The recommendation should be "Jacket" if temperature is
less than 60, and "T-shirt" otherwise.
ANSWER
var outfitRecommendation;
if (temperature < 60)
outfitRecommendation = "Jacket";
else
outfitRecommendation = "T-shirt";
4
AP Computer Science Principles Unit 4: Conditionals
6. A customer can only place an order if the shop is open. The order should not be
processed if the selected drink is not available. The total cost of the order should be
calculated based on the drink price. The system should log a message if the order cannot
be processed.
var shopOpen; // Indicates if the coffee shop is currently open
var drinkAvailable; // Indicates if the selected drink is available
var drinkPrice = 5.00; // Price of the selected drink
var orderQuantity = 2; // Number of drinks ordered
(2 answers shown)
if ( !shopOpen || !drinkAvailable )
[Link]("Cannot complete order.");
else
price = drinkPrice * orderQuantity;
if (shopOpen && drinkAvailable)
price = drinkPrice * orderQuantity;
else
[Link] ("Cannot complete order.");
5
AP Computer Science Principles Unit 4: Conditionals
7. The damage dealt by a Pokémon attack depends on the type of the attacker and the
type of the defender.
● If the attacker is a Fire type and the defender is a Grass type, the damage is
doubled.
● If the attacker is a Water type and the defender is a Fire type, the damage is
tripled.
● If the attacker is a Grass type and the defender is a Water type, the damage is
halved.
● Otherwise, the finalDamage does not change.
var attackerType = "Fire"; // Type of the attacking Pokémon
var defenderType = "Grass"; // Type of the defending Pokémon
var finalDamage = 3; // Initialize final damage
if (attackerType == "Fire" && defenderType == "Grass")
finalDamage= finalDamage * 2;
else if (attackerType == "Water" && defenderType == "Fire")
finalDamage = finalDamage * 3;
else if (attackerType == "Grass" && defenderType == "Water")
finalDamage = finalDamage / 2;
else
finalDamage = finalDamage;