Interactive online courses HTML Academy
2026-03-09 14:09 Diff

Congratulations, you completed the first baptism by fire of the first program! Now the Muffin will give you tasks for more complex programs, programs with conditions.

And you right away get the first task: you need to develop a program called “Can I Eat More?”. This is a very important issue for everyone who is preparing for the exhibition. The program uses the following data:

  • daily amount of food (variable dayLimit);
  • weight of food eaten today (variable foodInGrams).

If the Muffin ate less than the daily amount, then he can eat more. If he exceeded the norm, then he can’t eat anymore.

So far, we have written linear programs that always executed the same commands. Now we need the program to become non-linear: depending on different conditions, it must perform different commands. How to teach the program to check the conditions and make decisions based on the results of the checks?

To check conditions, use the conditional operator if:

if (condition) { action; }

Here, the “condition” is an expression that returns true or false, and the “actions” inside curly brackets are commands that are executed if the condition is satisfied.

First, let’s write a condition that will check if the Muffin ate less than the daily amount of food. And if the condition is met, we’ll tell the Muffin that he can eat a little more.