Conditionals in JavaScript - Hacks
Three scaffolded hacks to practice selection (if, else if, else) in JavaScript. No nesting.
๐ 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 / elseand combine comparisons with&&.
Tips: use parseInt(..., 10); inclusive means >= and <=.
%%js
// Starter code
var num = 7; // Change this number to test different inputs
//Complete the code below according to the rules above
๐ก Hack 2 โ Grade Evaluator (Intermediate)
Requirements (spec):
- Ask for a grade
0โ100. - If
grade >= 75, print two lines:You get extra credit!thenHave a good day. - Otherwise, print
Have a good dayonly.
%%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
๐ต 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"andage >= 16โGeneral Entrance - Else if
has_ticket === "yes"andage < 16โMinor Entrance (with guardian) - Else โ
No Entrance
Tip: normalize strings with .trim().toLowerCase().
%%js
// Starter code (student edits this cell)
let age = 15; // Change the age
let has_ticket = "yes"; // Change to "yes" or "no"
let vip = "no"; // 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"
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?
๐งพ 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?
- What would you like to add to the lesson that would help you better understand the material?