Math basics Flashcards

1
Q

What are the basic math operators?

A
  • + (Addition) - Adds two numbers together. 6 + 9
  • - (Subtraction) - Subtracts the right number from the left. 20 - 15
  • * (Multiplication) - Multiplies two numbers together. 3 * 7
  • / (Division) - Divides the left number by the right. 10 / 5
  • % (Remainder, sometimes called modulo) - Returns the remainder left over after you’ve divided the left number into a number of integer portions equal to the right number. 8 % 3 (Returns 2 which is the reminder of 8/3)
  • ** (Exponent) - Raises a base number to the exponent power, that is, the base number multiplied by itself, exponent times. 5 ** 2 (returns 25, which is the same as 5 * 5).
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

What is the operator precedence in JavaScript?

A

Operator precedence in JavaScript is the same as is taught in math classes in school — multiply and divide are always done first, then add and subtract (the calculation is always evaluated from left to right).

If you want to override operator precedence, you can put parentheses around the parts that you want to be explicitly dealt with first. For example:

(num2 + num1) / (8 + 2);
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

Explain the increment (++) operator

A

The increment (++) operator increments (adds one to) its operand and returns the value before or after the increment, depending on where the operator is placed.

et x = 3;
const y = x++;

console.log(`x:${x}, y:${y}`);
// Expected output: "x:4, y:3"

let a = 3;
const b = ++a;

console.log(`a:${a}, b:${b}`);
// Expected output: "a:4, b:4"

Syntax

x++
\++x

The ++ operator is overloaded for two types of operands: number and BigInt. It first coerces the operand to a numeric value and tests the type of it. It performs BigInt increment if the operand becomes a BigInt; otherwise, it performs number increment.

If used postfix, with operator after operand (for example, x++), the increment operator increments and returns the value before incrementing.

If used prefix, with operator before operand (for example, ++x), the increment operator increments and returns the value after incrementing.

The increment operator can only be applied on operands that are references (variables and object properties; i.e. valid assignment targets). ++x itself evaluates to a value, not a reference, so you cannot chain multiple increment operators together.

\++(++x); // SyntaxError: Invalid left-hand side expression in prefix operation
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

What is the value of the resulting variables in the following code?

let x = 3;
const y = ++x;

let x2 = 3n;
const y2 = ++x2;
A

x is 4; y is 4
x2 is 4n; y2 is 4n

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

What is the value of the resulting variables in the following code?

let x = 3;
const y = x++;

let x2 = 3n;
const y2 = x2++;
A

x is 4; y is 3
x2 is 4n; y2 is 3n

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

Explain the decrement (--) operator

A

The decrement (--) operator decrements (subtracts one from) its operand and returns the value before or after the decrement, depending on where the operator is placed.

let x = 3;
const y = x--;

console.log(`x:${x}, y:${y}`);
// Expected output: "x:2, y:3"

let a = 3;
const b = --a;

console.log(`a:${a}, b:${b}`);
// Expected output: "a:2, b:2"

Syntax

x--
--x

The -- operator is overloaded for two types of operands: number and BigInt. It first coerces the operand to a numeric value and tests the type of it. It performs BigInt decrement if the operand becomes a BigInt; otherwise, it performs number decrement.

If used postfix, with operator after operand (for example, x--), the decrement operator decrements and returns the value before decrementing.

If used prefix, with operator before operand (for example, --x), the decrement operator decrements and returns the value after decrementing.

The decrement operator can only be applied on operands that are references (variables and object properties; i.e. valid assignment targets). --x itself evaluates to a value, not a reference, so you cannot chain multiple decrement operators together.

--(--x); // SyntaxError: Invalid left-hand side expression in prefix operation
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

What is the value of the resulting variables in the following code?

let x = 3;
const y = x--;

let x2 = 3n;
const y2 = x2--;
A

x is 2; y is 3
x2 is 2n; y2 is 3n

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

What is the value of the resulting variables in the following code?

let x = 3;
const y = --x;

let x2 = 3n;
const y2 = --x2;
A

x is 2; y = 2
x2 is 2n; y2 is 2n

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

Please list and explain all assignment operators

A
  • += (Addition assignment) - Adds the value on the right to the variable value on the left, then returns the new variable value. Example: x += 4; is equivalent to x = x + 4;.
  • -= (Subtraction assignment) - Subtracts the value on the right from the variable value on the left, and returns the new variable value. Example: x -= 3; is equivalent to x = x - 3;.
  • *= (Multiplication assignment( - Multiplies the variable value on the left by the value on the right, and returns the new variable value. Example x *= 3; is equivalent to x = x * 3;.
  • /= (Division assignment) - Divides the variable value on the left by the value on the right, and returns the new variable value. Example x /= 5; is equivalent tox = x / 5;
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

Please list an explain all comparison operators

A
  • === (Strict equality) - Tests whether the left and right values are identical to one another. Example: 5 === 2 + 4
  • !== (Strict-non-equality) - Tests whether the left and right values are not identical to one another. Example: 5 !== 2 + 3
  • < (Less than) - Tests whether the left value is smaller than the right one. Example: 10 < 6
  • > (Greater than) - Tests whether the left value is greater than the right one. Example: 10 > 20
  • <= (Less than or equal to) - Tests whether the left value is smaller than or equal to the right one. Example: 3 <= 2
  • >= (Greater than or equal to) - Tests whether the left value is greater than or equal to the right one. 5 >= 4
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

What is the Math namespace?

A

The Math namespace object contains static properties and methods for mathematical constants and functions.

Imprtant: Math works with the Number type. It doesn’t work with BigInt.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

What does Math.abs(x) do?

A

Returns the absolute value of x.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

What does Math.ceil(x) do?

A

Returns the smallest integer greater than or equal to x.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

What does Math.floor(x) do?

A

Returns the largest integer less than or equal to x.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

What does Math.max(x1, x2, ...) do?

A

Returns the largest of the numbers given as input parameters.

Returns NaN if any of the parameters is or is converted into NaN. Returns -Infinity if no parameters are provided.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

What does Math.min(x1, x2, ...) do?

A

Returns the smallest of the numbers given as input parameters, or Infinity if there are no parameters.

Returns NaN if any of the parameters is or is converted into NaN. Returns Infinity if no parameters are provided.

17
Q

What does Math.pow() do?

A

Returns the value of a base raised to a power.

Math.pow() is equivalent to the ** operator, except Math.pow() only accepts numbers.

18
Q

What does Math.random() do?

A

Returns a floating-point, pseudo-random number that’s greater than or equal to 0 and less than 1, with approximately uniform distribution over that range — which you can then scale to your desired range. The implementation selects the initial seed to the random number generation algorithm; it cannot be chosen or reset by the user.

Note: Math.random() does not provide cryptographically secure random numbers. Do not use them for anything related to security

19
Q

How can you get a random integer between 2 values?

A
function getRandomInt(min, max) {
  min = Math.ceil(min);
  max = Math.floor(max);
  return Math.floor(Math.random() * (max - min) + min); // The maximum is exclusive and the minimum is inclusive
}
20
Q

What does Math.round() do?

A

Returns the value of a number rounded to the nearest integer.

If the fractional portion of the argument is greater than 0.5, the argument is rounded to the integer with the next higher absolute value. If it is less than 0.5, the argument is rounded to the integer with the lower absolute value. If the fractional portion is exactly 0.5, the argument is rounded to the next integer in the direction of +∞.

21
Q

What does Math.trunc() do?

A

Returns the integer part of a number by removing any fractional digits.

22
Q

How can you round a number to a fixed number of decimal places?

A

With toFixed() method.

const lotsOfDecimal = 1.766584958675746364;
const twoDecimalPlaces = lotsOfDecimal.toFixed(2); // 1.77