HTML Academy
Strict number comparison
Conditions8/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?”
If the condition is not fulfilled
  • Sign up
  • Log in

Loading…
Everything will be ready in few seconds

  • Theory
  • Theory
  • Comments

Condition-based actions

Time to work, a letter from the boss!

Second task: you need to improve the program “Can I Eat More?”. Input data is the same:

  • daily amount of food, in the variable dayLimit;
  • weight of food eaten today, in the variable foodInGrams.

But the logic needs to be changed. The program should show how many grams of food can still be eaten today. If the amount of eaten food is greater than the daily amount, then the program should show zero.

First, let’s think over the solution algorithm:

  1. let’s set up a new variable to store the amount of food that can still be eaten;
  2. let’s check if the amount of eaten food is less than the norm;
  3. if so, we subtract from the norm the amount of eaten food, and save the result in the new variable;
  4. if not, we write a zero in the new variable;
  5. log a beautiful informative message.

As you can see, inside the conditions, you can and you should perform operations with variables, and not just log messages.

Comments

  • script.js
JavaScript
var foodInGrams = muffin.ask('How many grams of food have you eaten today? Just be honest?'); var dayLimit = 80; if (foodInGrams < dayLimit) { console.log('You can eat more!'); } else { console.log('Not a piece more!'); }

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

Now we need to calculate how much food can be eaten if the amount eaten is less than the daily amount.

  1. Add declaring of the foodToEat variable immediately after the dayLimit variable is declared.
  2. Replace the logged message console.log ('You can eat more'); with foodToEat = dayLimit - foodInGrams;.

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.