Nested Conditionals in Javascript - Homework
Categories: JavascriptApply your skills of multilayered conditionals and combine all you've learned so far.
Nested Conditionals Homework: Javascript
Complete the following three problems to practice working with nested conditionals. Each problem increases in difficulty. All problems must be completed in JavaScript.
Problem 1: Complete the Shipping Cost Calculator (Easy)
An online store calculates shipping costs based on the order total and membership status. The partially completed code below needs to be finished.
Rules:
- If order total is $50 or more:
- Free shipping for members
- $5 shipping for non-members
- If order total is less than $50:
- $3 shipping for members
- $8 shipping for non-members
Your task: Complete the nested conditional so that when you run the code with the given initial values (orderTotal = 45 and isMember = true), the output shows a shipping cost of $3.
%%js
// Initial conditions - DO NOT CHANGE THESE
let orderTotal = 45;
let isMember = true;
let shippingCost = 0;
console.log(`Order Total: $${orderTotal}`);
console.log(`Member: ${isMember}`);
// YOUR CODE HERE: Complete the nested conditional
if (orderTotal >= 50) {
if (isMember){
console.log(shippingCost = 0);
} else {
console.log(shippingCost = 5)
}
} else {
if (isMember) {
console.log(shippingCost = 3);
}else {
console.log(shippingCost = 8);
}
}
console.log(`Shipping Cost: $${shippingCost}`);
// Expected output with given values: Shipping Cost: $3
<IPython.core.display.Javascript object>
Problem 2: Build a Restaurant Recommendation System (Medium)
Create a system that recommends a restaurant based on the user’s budget and cuisine preference.
Requirements:
- If budget is “high” (over $30 per person):
- If cuisine is “italian”: recommend “Bella Notte”
- If cuisine is “japanese”: recommend “Sakura Palace”
- For any other cuisine: recommend “The Grand Bistro”
- If budget is “medium” ($15-$30 per person):
- If cuisine is “italian”: recommend “Mario’s Pizzeria”
- If cuisine is “japanese”: recommend “Tokyo Express”
- For any other cuisine: recommend “Downtown Diner”
- If budget is “low” (under $15 per person):
- Recommend “Food Court” regardless of cuisine preference
Your task: Write the complete nested conditional structure. Store the recommendation in the recommendation variable.
%%js
// Test variables
let budgetPerPerson = 25;
let cuisine = "japanese";
let recommendation = "";
console.log(`Budget per person: $${budgetPerPerson}`);
console.log(`Preferred cuisine: ${cuisine}`);
// YOUR CODE HERE: Write the complete nested conditional
if (budgetPerPerson >= 30){
if (cuisine === "italian"){
console.log(recommendation = "Bella Notte")
}else if (cuisine === "japanese"){
console.log(recommendation = "Sushi World")
}else{
console.log(recommendation = "The Grand Bistro")
}
} else if (budgetPerPerson >= 15 && budgetPerPerson < 30 ){
if (cuisine === "italian"){
console.log(recommendation = "Mario's Pizzeria")
}else if(cuisine === "japanese"){
console.log(recommendation = "Tokyo Express")
}else{
console.log(recommendation = "Downtown Diner")
}
} else if (budgetPerPerson < 15){
console.log(recommendation = "Food Court")
}
console.log(`\nRecommendation: ${recommendation}`);
// Test your code with these scenarios:
// budgetPerPerson=35, cuisine="italian" → "Bella Notte"
// budgetPerPerson=25, cuisine="japanese" → "Tokyo Express"
// budgetPerPerson=20, cuisine="mexican" → "Downtown Diner"
// budgetPerPerson=10, cuisine="italian" → "Food Court"
<IPython.core.display.Javascript object>
Problem 3: Design a Smart Home Thermostat System (Hard)
You’re designing the logic for a smart thermostat that automatically adjusts temperature based on multiple factors.
Word Problem:
The thermostat needs to decide what action to take based on:
- Current temperature
- Whether anyone is home
- Time of day (represented as “day” or “night”)
- Energy saving mode (on or off)
Logic Requirements:
- If someone is home:
- During the day:
- If temp is above 75°F: set action to “cooling” and targetTemp to 72
- If temp is below 68°F: set action to “heating” and targetTemp to 70
- Otherwise: set action to “maintaining” and keep current temp
- During the night:
- If temp is above 72°F: set action to “cooling” and targetTemp to 68
- If temp is below 65°F: set action to “heating” and targetTemp to 68
- Otherwise: set action to “maintaining” and keep current temp
- During the day:
- If no one is home:
- If energy saving mode is ON:
- If temp is above 80°F: set action to “cooling” and targetTemp to 78
- If temp is below 60°F: set action to “heating” and targetTemp to 62
- Otherwise: set action to “off” and targetTemp to current temp
- If energy saving mode is OFF:
- Set action to “maintaining” and targetTemp to 70
- If energy saving mode is ON:
Your task: Design the complete nested conditional algorithm from scratch. You’re given the framework below with initial values, but NO code. Write the entire logic yourself.
%%js
// Test variables
let currentTemp = 78;
let isHomeOccupied = true;
let timeOfDay = "day"; // "day" or "night"
let energySavingMode = false;
// Variables to set
let action = ""; // "heating", "cooling", "maintaining", or "off"
let targetTemp = currentTemp;
console.log("=== Smart Thermostat Status ===");
console.log(`Current Temperature: ${currentTemp}°F`);
console.log(`Home Occupied: ${isHomeOccupied}`);
console.log(`Time of Day: ${timeOfDay}`);
console.log(`Energy Saving Mode: ${energySavingMode}`);
console.log();
// === Smart Thermostat Logic ===
if (isHomeOccupied) {
if (timeOfDay === "day") {
if (currentTemp > 75) {
action = "cooling";
targetTemp = 72;
} else if (currentTemp < 68) {
action = "heating";
targetTemp = 70;
} else {
action = "maintaining";
}
} else { // night
if (currentTemp > 72) {
action = "cooling";
targetTemp = 68;
} else if (currentTemp < 65) {
action = "heating";
targetTemp = 68;
} else {
action = "maintaining";
}
}
} else { // Home not occupied
if (energySavingMode) {
if (currentTemp > 80) {
action = "cooling";
targetTemp = 78;
} else if (currentTemp < 60) {
action = "heating";
targetTemp = 62;
} else {
action = "maintaining";
}
} else {
action = "maintaining";
targetTemp = 70;
}
}
console.log("=== Thermostat Action ===");
console.log(`Action: ${action}`);
console.log(`Target Temperature: ${targetTemp}°F`);
<IPython.core.display.Javascript object>
Reflection Questions
After completing all three problems, answer these questions:
- Which problem was the most challenging and why?
The third one was very challenging, it involved multiple conditions and possibilities that are nested within, I struggled to create the function correctly, especially on the part when someone was home and I had to set the target Temp, and do maintain the temp if it is just right. Also extinguishing the day and night, for both when someone was and wasnt home, was very challenging.
- How did you decide on the structure of your nested conditionals in Problem 3?
I was unsure, I just started off by creating a condition if the houes is occupied or not, if yes, then daytime, then whenever energySavingMode was on or not, similar structure for the 2nd one. I am not sure if my code work or not, I am unsure about this one.
- Can you think of a real-world situation where you would need even MORE levels of nesting than Problem 3?
Probably: If I dont study for test 3 for my AP CALC BC class, I will probably not be prepared for the final. If I dont have good foundation of knowledge for the final, I will likely fail the course. If I fail the course for this Tri, I will likely not have enough knowledge to get a good grade for the class this year. If I dont have enough knowledge and is getting bad grades, I will likely not do good on the AP exam. If I fail the class and the AP exam, I will likely have a bad record and I wont be able to get to good colleges. If I dont go to a college, I wont be able to find a good job. If I dont get a good job, I will likely not have enough money to live comfortably.