Conditionals in JavaScript - Hacks
Categories: JavascriptThree 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 <=.
%%javascript
if (num > 0 && num < 10){
console.log(num);
console.log("Goodbye");
} else {
console.log("Goodbye!");
}
<IPython.core.display.Javascript object>
🟡 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.
%%javascript
// Starter code
let grade = 85; // Change this grade to test different cases
console.log(grade);
if (grade > 75){
console.log("You get extra Credit!");
console.log("Have a good day");
} else {
console.log("Have a good day");
}
<IPython.core.display.Javascript object>
🔵 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().
%%javascript
let age = 15; // Change the age
let has_ticket = "yes"; // Change to "yes" or "no"
let vip = "no"; // Change to "yes" or "no"
if (vip == "yes"){
console.log("VIP Entrance");
}
else if (age >= 16 && has_ticket == "yes"){
console.log("General Entrance");
}
else if (age <16 && has_ticket == "yes"){
console.log("Minor Entrance (with guardian)");
}
else {
console.log("No Entrance");
}
<IPython.core.display.Javascript object>
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. There were some obvious problems, both involving the order of the “if, else if, and else” and how i didn’t know “elif” is replaced by “else if” in JavaScript. The third one was a lot easier than I had expected, but 2nd one was harder because it was starting to introduce to the more complex parts, when I had to do multiple operations to set the elements in the conditional situation.
- 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 Real life examples we can use as conditions is probably about the game of chess, every move that follows, until the game ends is a condition with trillions of possibilities.
- How is javascript different from python in terms of conditionals? Javascripts were more simple and effective when organizing the conditionals, which was different because Python is usually more simple.
🧾 Turn-in checklist
- [Checked!] All three hacks run and match the rules above them.
- [Checked!] 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.
- [Checked!] You wrote a 2–3 sentence reflection in your portfolio:
- What conditional form did you use most? I used the Boolean a lot, to create the necessary elements of the conditioanl situation. For example, to create a ticket system in question #3, I had to list out the possibilities and what would happen.
- What would you like to add to the lesson that would help you better understand the material? I would still like to add on more and clearer definitions of important things like the “if, else if, and else” material within the conditionals, also there should be completed examples with the most important parts highlighted.