Since we've learned to write simple programs, let's talk about the very process of writing.
The program code should be organized in a certain way so that it is sufficiently clear and easy to maintain. Special sets of rules - standards - describe different aspects of code writing. The most common standard in JavaScript is Airbnb.
In any programming language, there are utilities known as linters. They ensure the code meets the standards. For example, ESLint analyzes JavaScript code.
Take a look at the example from the previous lesson:
console.log(8/2+5 - -3 / 2); // => 10.5
Linter won't be happy about it, because several rules have been violated:
In the last lesson we recognized that such an abundance of numbers and symbols may be confusing, and we decided to add parentheses purely for the sake of readability:
console.log(((8 / 2) + 5) - (-3 / 2)); // => 10.5
This code does not violate the rules, and the linter will "say nothing" as it were.
In previous exercise you probably did this:
console.log(70 * (3 + 4) / (8 + 2));
Is there any violation of the standard here?
Unfortunately, yes. This time, the *
and /
are in the same expression and there are no parentheses. You can solve this problem by adding additional parentheses. But at some point, too many parentheses can make the code incomprehensible again. At this point, you can divide the expression into separate parts. We will learn how to do this in the following lessons.
no-mixed-operators is just one of many rules. Other ones describe indentations, entity names, parentheses, mathematical operations, line length, and many other aspects. Each rule may seem small and insignificant, but together they form the basis of good code.
Code-Basics won't test your code with a linter now, but in your future Hexlet practice segments and in actual development, the linter will work and flag the bugs.
Print the result of "the difference between five squared and the product of three and seven". Place the parentheses to follow the no-mixed-operators
rule.
If you've reached a deadlock it's time to ask your question in the «Discussions». How ask a question correctly:
Tests are designed so that they test the solution in different ways and against different data. Often the solution works with one kind of input data but doesn't work with others. Check the «Tests» tab to figure this out, you can find hints at the error output.
It's fine. 🙆 One task in programming can be solved in many different ways. If your code passed all tests, it complies with the task conditions.
In some rare cases, the solution may be adjusted to the tests, but this can be seen immediately.
It's hard to make educational materials that will suit everyone. We do our best but there is always something to improve. If you see a material that is not clear to you, describe the problem in “Discussions”. It will be great if you'll write unclear points in the question form. Usually, we need a few days for corrections.
By the way, you can participate in courses improvement. There is a link below to the lessons course code which you can edit right in your browser.
Your exercise will be checked with these tests:
1import { expectOutput } from 'hexlet-basics/tests';
2
3const expected = '4';
4expectOutput(expected);
5
Teacher's solution will be available in: