Number and Math Methods Flashcards

1
Q

Return boolean check if argument is an integer

A

Number.isInteger( )

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

Return a number rounded to a number of decimal places

Why does it return a string?

A

myNumber.toFixed(numberOfdecimal places )

It returns a string because there is no way to count decimal places in bits (but you can in string indexes). So we have to convert to use .toFixed to convert to string then convert it back to a number.

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

Returna base 10 number, based off number argument and its number base

A

Number.parseInt(11, 2)
or just
parseInt()
// expected output

3

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

Round a number up to the next largest integer

Round a number down up to the next largest integer

A

Math.ceil(5.5) = 6

Math.floor(5.5) = 5

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

Return 1 if the argument is positive

Return -1 if the argument is negative

Return 0 if the argument is 0

A

Math.sign( )

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

Return the smallest number among the arguments

Return the largest number among the arguments

Do the same but return the max or min element in an array

A

Math.min() Math.max()

Math.min(…[1, 2]), Math.max(…array)

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

What does
Math.min()
do?
and/or return?
What happens if an argument is not a number?

A

The static function Math.min() returns the lowest-valued number passed into it.
Arguemnts are converted to numbers. If any of them are not numbers, it returns NaN.

Math.min()
Math.min(value0)
Math.min(value0, value1)
Math.min(value0, value1, … , valueN)

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

What does
Math.max()
do?
and/or return?

A

The Math.max() function returns the largest of the zero or more numbers given as input parameters, or NaN if any parameter isn’t a number and can’t be converted into one.

Math.max()
Math.max(value0)
Math.max(value0, value1)
Math.max(value0, value1, /* … ,*/ valueN)

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

What does
Math.ceil()
do?
and/or return?

A

The Math.ceil() function always rounds a number up to the next largest integer.

Math.ceil(x)

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

What does
Math.floor()
do?
and/or return?

A

The Math.floor() function returns the largest integer less than or equal to a given number.

Math.floor(x)

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

What does
Number.prototype.toFixed()
do?
and/or return?

A

The toFixed() method formats a number using fixed-point notation.
Returns a string with a given number of float points.
toFixed()
toFixed(digits)

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

What does
Number.isInteger()
do?
and/or return?

A

The Number.isInteger() method determines whether the passed value is an integer.

Number.isInteger(value)

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

What does
Number.MAX_VALUE
do?
and/or return?

A

The Number.MAX_VALUE property represents the maximum numeric value representable in JavaScript.

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

What does
Number.prototype.toString()
do?
and/or return?

A

The toString() method returns a string representing the specified Number object.

toString()
toString(radix)

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

What does
Math.trunc() (not used by internet explorer)
do?
and/or return?

A

The Math.trunc() function returns the integer part of a number by removing any fractional digits.
Basically it rounds down (unless if it’s got tons of 9s

Math.trunc(x)

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

Method to remove decimals from a float (basically rounds down to the integer)

A

Math.trunc() (not used by internet explorer)

17
Q

Return the max value

A

Number.MAX_VALUE

18
Q
What's the difference between:
Math.ceil()
Math.floor()
Math.trunc()
Number.prototype.toFixed()
A

Math.ceil() and Math.floor() round to nearest integer.

Math.trunc() removes floats. It’s like Math.floor() for positive numbers and Math.ceil() for negative numbers.

.toFixed() returns a string and can round to an float you want

19
Q

Existing method to check if something is NaN (2 methods, what’s the difference?

A

isNaN() (converts to a number)

Number.isNaN() (does not convert to a number)

20
Q

Check if a number is finite (not NaN, infinity) (2 ways)

A

Number.isFinite() (just for numbers)

isFinite() (converts values to numbers first)

21
Q

What is return from:
isFinite(‘word’)
isFinite(‘’)

A

False, non numbers are not finite

True (empty and space only strings are converted to 0 when converted to numbers)

22
Q

What does isFinite() do? (Number.isFinite())

What does inNaN() do? (Number.inNaN())

A

Method to check if something is NaN

Check if a number is finite (not NaN, infinity)

23
Q

What does Object.is() do?

A

special built-in method Object.is that compares values like ===, but is more reliable for two edge cases:

  1. It works with NaN: Object.is(NaN, NaN) === true, that’s a good thing.
  2. Values 0 and -0 are different: Object.is(0, -0) === false, technically that’s true, because internally the number has a sign bit that may be different even if all other bits are zeroes.
24
Q

What is return from:

  • Object.is(NaN, NaN)
  • NaN == NaN
  • Object.is(0, -0)
  • 0 == -0
A

True
False
False
True

25
Q

What is returned from:

  • parseFloat(‘12.3.4’)
  • 12.3.4
A

12.3

error

26
Q

What is returned from:
parseInt(‘a123’)
parseInt(‘1a123’)

A

NaN

1

27
Q

Return 2^9 (2 ways)

A

Math.pow(2, 9)

2**9

28
Q

What does Math.pow() do?

A

returns a number to the power of another number

also:
x(star)(star)y does the same
x(star)(star)y is faster probably

29
Q

What is returned from:

1. 35).toFixed(1
(6. 35).toFixed(1)

A

1.4
6.3
You have to do this sort of thing:
( Math.round(6.35 * 10) / 10)

30
Q

Name all Number() methods (10 methods, 3 categories + 1 other)

Name the global funtions that are also Number() methods (Number.method() vs method(), how do the Global object methods differ)
(4 methods)

A

[[[[ —> indicates a global method

Check number to see if it is something (Return Boolean)
—> isFinite() Checks whether a value is a finite number (global converts to number)
—> isNaN() Checks whether a value is Number.NaN (global converts to number)
isSafeInteger() Checks whether a value is a safe integer
isInteger() Checks whether a value is an integer

Parse Number to see if it has something
—> Number.parseFloat() Returns float found in string (global is exact same)
—> Number.parseInt() Returns intfound in string
(global is exact same)

Convert Number into something else
toExponential(x) Converts a number into an exponential notation
toFixed(x) Formats a number with x numbers of digits after the decimal point
toLocaleString() Converts a number into a string, based on the locale settings
toPrecision(x) Formats a number to x length
toString() Converts a number to a string

valueOf() Returns the primitive value of a number

31
Q

Name all Math() methods
(14, + 13 are regular, arc and hyperbolic trig operations)

5 categories + 1 TRIG

A
RANDOM 1
ROUND OR CUT 4
EXPONENT LOG LN 5
NEGATIVE ABSOLUTE 2
SELECT 2
TRIG 13
.
.
.
.
.
.
.
.
.
.
.
.
.
.
RANDOM 1
Math.random()	Returns a random number between 0 and 1

ROUND OR CUT 4
Math.round(x) Rounds x to the nearest integer
Math.trunc(x) Returns the integer part of a number (x)

Math.ceil(x) Returns x, rounded upwards to the nearest integer
Math.floor(x) Returns x, rounded downwards to the nearest integer

EXPONENT LOG LN
Math.pow(x, y) Returns the value of x to the power of y
Math.sqrt(x) Returns the square root of x
Math.cbrt(x) Returns the cubic root of x

Math.log(x) Returns the natural logarithm (base E) of x
Math.exp(x) Returns the value of E^x

NEGATIVE POSITIVE
Math.sign(x) Returns if x is negative, null or positive (-1, 0, 1)
Math.abs(x) Returns the absolute value of x

SELECT
Math.max(x, y, z, …, n) Returns the number with the highest value
Math.min(x, y, z, …, n) Returns the number with the lowest value

TRIG
Math.sin(x) Returns the sine of x (x is in radians)
Math.cos(x) Returns the cosine of x (x is in radians)
Math.tan(x) Returns the tangent of an angle

Math.sinh(x) Returns the hyperbolic sine of x
Math.cosh(x) Returns the hyperbolic cosine of x
Math.tanh(x) Returns the hyperbolic tangent of a number

Math.asin(x) Returns the arcsine of x, in radians
Math.acos(x) Returns the arccosine of x, in radians
Math.atan(x) Returns the arctangent of x as a numeric value between -PI/2 and PI/2 radians
Math.atan2(y, x) Returns the arctangent of the quotient of its arguments

Math.asinh(x) Returns the hyperbolic arcsine of x
Math.acosh(x) Returns the hyperbolic arccosine of x
Math.atanh(x) Returns the hyperbolic arctangent of x