Here’s an example of a javascript hack that simulates fitness levels. As you can see, the code utilizes if and else statements. If statements and else statements are commonly used together. If statements check if a statement is true, while else statements will check if it is false.

%%javascript
function assessFitnessLevel(pushUps) {
    let fitnessLevel;
    if (pushUps >= 50) {
        fitnessLevel = "Elite";
    } else {
        if (pushUps >= 30) {
            fitnessLevel = "Advanced";
        } else {
            if (pushUps >= 15) {
                fitnessLevel = "Intermediate";
            } else {
                if (pushUps >= 1) {
                    fitnessLevel = "Beginner";
                } else {
                    fitnessLevel = "Unfit";
                }
            }
        }
    }
    return fitnessLevel;
}
// Example usage
const userPushUps = 28; // Change this value to test different inputs
console.log(`Fitness Level: ${assessFitnessLevel(userPushUps)}`);
<IPython.core.display.Javascript object>

Javascript Hack

  • In this hack, the if statement works with the else statement to determine the user’s fitness level. The if statement checks if the user has gotten more than the given number of pushups, if it doesn’t work, the else statement will be executed. The else statement will check if the user has gotten less than the number of pushups, if it doesn’t meet the requirements, the user will be classified as the lower level.

Here’s a short overview of nested conditionals:

if outer_condition:
    # Code for when outer_condition is true
    if inner_condition:
        # Code for when both outer_condition and inner_condition are true
    else:
        # Code for when outer_condition is true but inner_condition is false
else:
    # Code for when outer_condition is false
  File "/tmp/ipykernel_741876/1518511658.py", line 5
    else:
    ^
IndentationError: expected an indented block after 'if' statement on line 3
  • Notice that there are if and else statements inside of the system which makes it so that those statements depend on the outer function to be true. This is what makes them nested conditionals