Number and Math Methods Flashcards
Return boolean check if argument is an integer
Number.isInteger( )
Return a number rounded to a number of decimal places
Why does it return a string?
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.
Returna base 10 number, based off number argument and its number base
Number.parseInt(11, 2)
or just
parseInt()
// expected output
3
Round a number up to the next largest integer
Round a number down up to the next largest integer
Math.ceil(5.5) = 6
Math.floor(5.5) = 5
Return 1 if the argument is positive
Return -1 if the argument is negative
Return 0 if the argument is 0
Math.sign( )
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
Math.min() Math.max()
Math.min(…[1, 2]), Math.max(…array)
What does
Math.min()
do?
and/or return?
What happens if an argument is not a number?
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)
What does
Math.max()
do?
and/or return?
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)
What does
Math.ceil()
do?
and/or return?
The Math.ceil() function always rounds a number up to the next largest integer.
Math.ceil(x)
What does
Math.floor()
do?
and/or return?
The Math.floor() function returns the largest integer less than or equal to a given number.
Math.floor(x)
What does
Number.prototype.toFixed()
do?
and/or return?
The toFixed() method formats a number using fixed-point notation.
Returns a string with a given number of float points.
toFixed()
toFixed(digits)
What does
Number.isInteger()
do?
and/or return?
The Number.isInteger() method determines whether the passed value is an integer.
Number.isInteger(value)
What does
Number.MAX_VALUE
do?
and/or return?
The Number.MAX_VALUE property represents the maximum numeric value representable in JavaScript.
What does
Number.prototype.toString()
do?
and/or return?
The toString() method returns a string representing the specified Number object.
toString()
toString(radix)
What does
Math.trunc() (not used by internet explorer)
do?
and/or return?
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)
Method to remove decimals from a float (basically rounds down to the integer)
Math.trunc() (not used by internet explorer)
Return the max value
Number.MAX_VALUE
What's the difference between: Math.ceil() Math.floor() Math.trunc() Number.prototype.toFixed()
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
Existing method to check if something is NaN (2 methods, what’s the difference?
isNaN() (converts to a number)
Number.isNaN() (does not convert to a number)
Check if a number is finite (not NaN, infinity) (2 ways)
Number.isFinite() (just for numbers)
isFinite() (converts values to numbers first)
What is return from:
isFinite(‘word’)
isFinite(‘’)
False, non numbers are not finite
True (empty and space only strings are converted to 0 when converted to numbers)
What does isFinite() do? (Number.isFinite())
What does inNaN() do? (Number.inNaN())
Method to check if something is NaN
Check if a number is finite (not NaN, infinity)
What does Object.is() do?
special built-in method Object.is that compares values like ===, but is more reliable for two edge cases:
- It works with NaN: Object.is(NaN, NaN) === true, that’s a good thing.
- 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.
What is return from:
- Object.is(NaN, NaN)
- NaN == NaN
- Object.is(0, -0)
- 0 == -0
True
False
False
True
What is returned from:
- parseFloat(‘12.3.4’)
- 12.3.4
12.3
error
What is returned from:
parseInt(‘a123’)
parseInt(‘1a123’)
NaN
1
Return 2^9 (2 ways)
Math.pow(2, 9)
2**9
What does Math.pow() do?
returns a number to the power of another number
also:
x(star)(star)y does the same
x(star)(star)y is faster probably
What is returned from:
1. 35).toFixed(1
(6. 35).toFixed(1)
1.4
6.3
You have to do this sort of thing:
( Math.round(6.35 * 10) / 10)
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)
[[[[ —> 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
Name all Math() methods
(14, + 13 are regular, arc and hyperbolic trig operations)
5 categories + 1 TRIG
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