HTML Academy
Condition-based actions
Conditions9/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?”
Values ​​as a condition
  • Sign up
  • Log in

Loading…
Everything will be ready in few seconds

  • Theory
  • Theory
  • Comments

If the condition is not fulfilled

We calculated how much food can be eaten, if the daily amount is not exceeded.

All we have left to do is to calculate the amount of food that can be eaten if the eaten amount exceeds the norm. This is the easiest task, because you just need to equate the value to zero.

And as the last step, you need to log a beautifully formatted message. You already know how to do it.

Comments

  • script.js
JavaScript
var foodInGrams = muffin.ask('How many grams of food have you eaten today? Just be honest?'); var dayLimit = 80; var foodToEat; if (foodInGrams < dayLimit) { foodToEat = dayLimit - foodInGrams; } 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

Let’s complete our work on the program.

  1. Instead of the console.log('Not a piece more!'); command, set the variable foodToEat to zero value.
  2. After the else block, log in the console a message made up of three fragments: string 'You can eat ', variable foodToEat, string ' more grams.'.

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.