🌐 CSP 3.6 Hacks β€” Conditionals (JavaScript)

You will complete three small programs that use selection with if, else if, else

Answers to common errors:

  • Please select the JavaScript kernel when running the code, do NOT select python
  • If the code reports random errors try pressing restart at the top bar
  • If you get stuck on a problem, make comments about what you understand so far and what you are stuck on

🟒 Hack 1 β€” Number Range Checker (Beginner)

Requirements (spec):

  • Ask for a number.
  • If the number is between 0 and 10 (inclusive), print the number then print Goodbye.
  • Otherwise, print only Goodbye!.
  • Use one if / else and combine comparisons with &&.

Tips: use parseInt(..., 10); inclusive means >= and <=.

%%js 
// Starter code 
var num = 17;  // Change this number to test different inputs

// Complete the code below according to the rules above
if (num >= 0 && num <= 10) {
  console.log(num);
  console.log("Goodbye");
} else {
  console.log("Goodbye!");
}

Javascript Output in Console

Console Output

🟑 Hack 2 β€” Grade Evaluator (Intermediate)

Requirements (spec):

  • Ask for a grade 0–100.
  • If grade >= 75, print two lines: You get extra credit! then Have a good day.
  • Otherwise, print Have a good day only.
%%js 
// Starter code 
let grade = 85;  // Change this grade to test different cases

// TODO:
// If grade >= 90 β†’ print "A" then "Have a good day"
// If grade >= 87 but < 90 β†’ print "Can be rounded to an "A", then "Have a good day"
// Else β†’ print "Have a good day"

//Complete the code below according to the rules above
// Check grade and print appropriate messages
if (grade >= 90) {
  console.log("A");
  console.log("Have a good day");
} else if (grade >= 70 && grade < 90) {
  console.log("Can be rounded to an A");
  console.log("Have a good day");
} else if (grade < 70 && grade >= 50) { //THIS IS MY CHANGE
  console.log("See me after class");
} else {
  console.log("Have a good day");
}

Javascript Output in Console

Console Output

πŸ”΅ Hack 3 β€” Access Pass (Advanced)

Inputs: age (number), has_ticket ("yes"/"no"), vip ("yes"/"no")

Rules

  • If vip === "yes" β†’ VIP Entrance
  • Else if has_ticket === "yes" and age >= 16 β†’ General Entrance
  • Else if has_ticket === "yes" and age < 16 β†’ Minor Entrance (with guardian)
  • Else β†’ No Entrance

Tip: normalize strings with .trim().toLowerCase().

%%js 
// Starter code (student edits this cell)
let age = 19;           //  Change the age
let paid_member = "yes"; //  Change to "yes" or "no"
let vip = "yes";         //  Change to "yes" or "no"

// TODO: Implement ladder using if / else if / else
// Rules:
// - VIP = "yes" β†’ "VIP Entrance"
// - Ticket = "yes" and age >= 16 β†’ "General Entrance"
// - Ticket = "yes" and age < 16 β†’ "Minor Entrance (with guardian)"
// - Otherwise β†’ "No Entrance"

// Complete the code below according to the rules above

// Normalize string inputs
paid_member = paid_member.trim().toLowerCase();
vip = vip.trim().toLowerCase();

// Implement ladder using if / else if / else
if (vip === "yes") {
  console.log("VIP Entrance");
} else if (paid_member === "yes" && age >= 16) {
  console.log("General Entrance");
} else if (paid_member === "yes" && age < 16) {
  console.log("Minor Entrance (with guardian)");
} else {
  console.log("No Entrance");
}

Javascript Output in Console

Console Output


Reflection: Think about the following question. Answer in 3-4 sentences

  • Did any of the question trip you up? If yes, explain the problem and your solution. If no, give a brief, 1-2 sentence summary of conditionals.
  • Can you think of real life examples where we would use conditionals? Please do NOT use one of the hacks as a real life example
  • How is javascript different from python in terms of conditionals?
  1. Did any of the questions trip you up? No, I didn’t have any problems. Conditionals are statements that let a program make decisions. If a certain condition is true, one block of code runs. Otherwise, another does.

  2. Real-life examples of conditionals: A phone could check if its battery is below 20% and then automatically turn on low power mode,if it’s not below 20%, then there is no low power mode.

  3. How JavaScript differs from Python in conditionals: In JavaScript, conditionals use parentheses and curly braces (if (x > 5) { ... }), while in Python, they use a colon and indentation (if x > 5:). Also, JavaScript uses === for strict equality, whereas Python just uses ==.


🧾 Turn-in checklist

  • All three hacks run and match the rules above them.
  • You included at least one changed element for at least one hack, this can be change of message, the range, or the input information (not the input, the prompt for the input). Do not just change the input and call that your personal change.
  • You wrote a 2–3 sentence reflection in your portfolio:
    • What conditional form did you use most? I used if else the most
    • What would you like to add to the lesson that would help you better understand the material? I think your lesson was pretty good. Maybe make the instructions less confusing