HTML Academy
Previous task
Conditions1/17
Back to the list of tasks
  • 1. Simple fork
  • 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 to take a walk for?”
Let’s use an alternative
  • Sign up
  • Log in

Loading…
Everything will be ready in few seconds

  • Theory
  • Theory
  • Comments

Simple fork

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.

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 in the condition to checkfoodInGrams < dayLimit (ate 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 replace the value of foodInGrams valuable with 60 to make sure that the condition is working.

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

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.