HTML Academy
Operations
Introduction to programming10/15
Back to the list of tasks
  • 1. Career start
  • 2. Making the program a bit more complicated
  • 3. Console log
  • 4. Data types
  • 5. Complex data types
  • 6. Unknown data
  • 7. Variables
  • 8. Declaring and assigning variables
  • 9. Operations
  • 10. Operations order
  • 11. A few more operations
  • 12. Release of Brekkie-meter v0.1, part 1
  • 13. Release of Brekkie-meter v0.1, part 2
  • 14. Summary of “Getting to know JavaScript”
  • 15. First program: MufFit v0.1
A few more operations
  • Sign up
  • Log in

Loading…
Everything will be ready in few seconds

  • Theory
  • Theory
  • Comments

Operations order

I hope you had no problems with the last task where you had to write the following code:

console.log(milkInGrams + waterInGrams - 150);

You can use several operations in a single command. The order of operation execution will depend on their priorities.

If an expression only contains arithmetic operations, their priority will be the same as taught at school: multiplication and division have the highest priority, addition and subtraction have the lowest one.

You can change the priority and the order of execution using round brackets. For example:

100 + 50 / 2;   // result is 125
(100 + 50) / 2; // result is 75

Of course, complex calculations can be simplified by breaking them down into several steps. For example, we could save the results of an operation into an intermediate variable and use it on the next step:

// This code will produce the same result
var totalWaterInGrams = milkInGrams + waterInGrams;
console.log(totalWaterInGrams - 150);

// as this one
console.log(milkInGrams + waterInGrams - 150);

It’s up to the developer if they want to use complex expressions made up of several operations or break the solution down into several steps consisting of simple operations. However, there is a golden rule: the simpler the code, the better.

Let’s try using more complex expressions.

Comments

  • script.js
JavaScript
var milkInGrams = 50;

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
  1. Calculate the caloric value of Muffin’s breakfast. To do this, save the result of the milkInGrams * (42 / 100) expression in variable breakfastCalories
  2. and then log breakfastCalories variable in the console.
  3. Find out how many grams of food Muffin can eat for lunch and dinner if his daily limit is 500 calories. In order to do that, save the result of the (500 - breakfastCalories) / 4 expression to the dryFeedInGrams variable (1 gram of food contains around 4 calories).
  4. Output the dryFeedInGrams variable to the console.

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.