Logical operations are expressions. So, you can combine them with other expressions.
Let's say, for example, we want to check if a number is even, i.e., a multiple of two. In programming we do this:
The remainder of a division is an elementary but crucial concept of arithmetic, algebra, and even number theory and cryptography. The idea is simple: divide a number into several equal groups, and if anything remains, it's the remainder of the division.
Split some candies equally among individuals:
The %
operator calculates the remainder of a division (not to be confused with division itself):
7 % 2; // 1
21 % 3; // 0
19 % 5; // 4
// Checking if even
10 % 2 // 10 is even, i.e. remainder is 0
9 % 2 // 9 odd, i.e. remainder is 1
Write a function to check whether a number is even:
const isEven = (number) => number % 2 === 0;
isEven(10); // true
isEven(3); // false
In one expression, we've combined the logical operator ===
(equality check) and the arithmetic operator %
.
Arithmetic operators have higher priority than logical ones. So, we first calculate the arithmetic expression number % 2
, and then its output is used in the logical comparison.
Literally, it means the following: "calculate the remainder of dividing number
by 2 and check if the remainder equals zero; then return the result of the equality check ".
Another example: write a function to check the first letter of a string is uppercase.
Algorithm:
const isFirstLetterInUpperCase = (string) => {
const firstLetter = string[0];
return firstLetter.toUpperCase() === firstLetter;
};
isFirstLetterInUpperCase('marmont'); // false
isFirstLetterInUpperCase('Robb'); // true
https://replit.com/@hexlet/js-basics-logica-operations
Write a function to check the format of a given phone number. If the number starts with +, then it's in the international format.
isInternationalPhone('89602223423'); // false
isInternationalPhone('+79602223423'); // true
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 { test, expect } from '@jest/globals';
2import f from './index.js';
3
4test('test', () => {
5 expect(f('89602223423')).toBe(false);
6 expect(f('+79602223423')).toBe(true);
7});
8
Teacher's solution will be available in: