HTML Academy
Previous task
Conditions1/17
Back to the list of tasks
  • 1. A simple branch
  • 2. Let’s use an alternative
  • 3. Simple comparisons
  • 4. Comparisons permitting equality
  • 5. Equality, inequality
  • 6. Strict string comparison
  • 7. Strict number comparison
  • 8. Condition-based actions
  • 9. If the condition is not fulfilled
  • 10. Values ​​as a condition
  • 11. Nested conditions
  • 12. Logical operators: &&, ||
  • 13. Logic traps
  • 14. Logical negation
  • 15. Let’s combine logical operators
  • 16. Summary of “Conditions”
  • 17. Second program: “How long should I walk?”
Let’s use an alternative
  • Sign up
  • Log in

Loading…
Everything will be ready in few seconds

  • Theory
  • Theory
  • Comments

A simple branch

Congratulations, you’ve written your first program and passed your first trial by fire! Now Muffin will give you tasks for more complex programs, programs with conditions.

And here is your 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 Muffin has eaten less than his daily allowance, he can eat more. If he has reached or exceeded it, 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 do we teach the program to check the conditions and make decisions based on the results of the checks?

To check conditions, use the if statement:

if (condition) {
  action;
}

Here, the “condition” is an expression whose value is tested as true or false, and the “actions” inside curly braces are commands that are executed if the condition is true.

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.

Comments

  • script.js
JavaScript
var foodInGrams = 120; var dayLimit = 80;

What didn’t you like in this task?

Thanks! We’ll fix everything at once!

Console

The code has changed, click “Run” or turn autorun on.

Result

Goalscompleted

Let’s start working on the “Can I Eat More?” program.

  1. After the dayLimit variable, add a conditional statement: if(true) { }.
  2. Now replace true with foodInGrams < dayLimit to check whether the amount eaten is less than the daily amount.
  3. Inside the curly brackets of the conditional statement, log the following message: console.log ('You can eat more!');.
  4. Now set the foodInGrams variable to 60 to see the condition become true.

Cookies ∙ Privacy ∙ License Agreement ∙ About ∙ Contacts ∙ © HTML Academy OÜ, 2019−2026

VISAMastercard

Log in

or

Forgot your password?

Sign up

Sign up

or
Log in

Restore access

Have you forgotten your password or lost access to your profile? Enter your email connected to your profile and we will send you a link to restore access.

Forgot to connect your email to the profile? Email us and we’ll help.