Numbers Knowledge Flashcards
‘8’ == 8; ‘8’ === 8;
true false
0.1 + 0.2
0.30000000000000004
Return the biggest number
What happens with bigger numbers?
Number.MAX_VALUE;
// 1.7976931348623157e+308
They are returned as Infinity
‘5’ + 1
‘1’ + 5
‘51’
‘15’
‘51’ > 6
6 < ‘51’
true
true
‘5’ * 1
‘5’ / ‘1’
5
5
‘12’ < ‘9’
true it only converts the first character of each if both sides are strings
Given an array of integers, find the one that appears an odd number of times.
There will always be only one integer that appears an odd number of times.
use an XOR gate ^ const a = 5; // 00000000000000000000000000000101 const b = 3; // 00000000000000000000000000000011
(a ^ b); // 00000000000000000000000000000110
const findOdd = (xs) => xs.reduce((a, b) => a ^ b);
Get infinity (2 ways)
Infinity
1/0
Evaluate Number(x) when x equals:
undefined
null
true and false
‘ 5 ‘
’ ‘
’ 2 hello ‘
NaN
0
1 and 0
5
0
NaN.
Evaluate:
‘2’ + 1
2 + ‘1’
‘21’
‘21’
Evlaluate:
2 + 2 + ‘1’
‘1’ + 2 + 2
41
122
Evaluate:
“6” / “2”
‘6’ - 2
‘a’ - 2
3
4
NaN
(+ is the only operator that will implicitly convert to strings. The others will only implicitly convert to numbers)
Evaluate the following with the + unary operator:
+2
+(-2)
+null
+undefined
+“true”
+true
It does the same thing as Number() essentially
2
-2
0
NaN
NaN
1
Evaluate a, b and c:
let a, b, c;
a = b = c = 2 + 2;
4
4
4
(but don’t do it)
Evaluate a and b:
let counter = 1;
let a = ++ counter
let b = counter++
a = 2 (add 1 to counter, then assign it to a)
b= 1 (assign counter to b, then add 1 to counter)
Evaluate
let counter = 0;
console. log(2 * counter++);
console. log(2 * ++counter);
0
4
For readability, instead of:
let counter = 1;
alert( 2 * counter++ );
what should you do?
let counter = 1;
alert( 2 * counter );
counter++;
What is returned from:
let a = (1 + 2, 3 + 4);
a;
// 7 (the result of 3 + 4)
Each of them is evaluated but only the result of the last one is returned.
What is returned from:
let a = 1 + 2, 3 + 4;
and what is returned from:
a;
//syntax error
What is returned from:
true + false
1
+ converts them to numbers
1 + 0
What is returned from:
” -9 “ - 5
-14
it first removes blank spaces from the beginning and end
This returns 12, make it return 3
let a = prompt("First number?", 1); let b = prompt("Second number?", 2);
alert(a + b); // 12
let a = +prompt(“First number?”, 1);
let b = +prompt(“Second number?”, 2);
alert(a + b); // 3
OR
let a = prompt("First number?", 1); let b = prompt("Second number?", 2);
alert(+a + +b); // 3
Boolean(0)
Boolean(‘0’)
0 == ‘0’
False
True
True
‘2’ > ‘12’
true
(both are strings so 2 > 1)
‘12’ < 2
false
(‘12’ is converted to a number when compared to a number)
alert( null > 0 );
alert( null == 0 );
alert( null >= 0 );
// (1) false
// (2) false (with equality, null is not converted to a number)
// (3) true (with comparisons, null is converted to a number)
What is returned from:
123.toString()
?
Syntax error.
it works with (123).toString()
or
let aNumber = 123
aNumber.toString()
Why? [incomplete]
What is an integer property?
a string that can be converted to a number and back to a string without changing
alert( String(Math.trunc(Number(“49”))) ); // “49”, same, integer property
alert( String(Math.trunc(Number(“+49”))) ); // “49”, not same “+49” ⇒ not integer property
alert( String(Math.trunc(Number(“1.2”))) ); // “1”, not same “1.2” ⇒ not integer property
What is the value of x in the following:
let x = 123_123
let x = 123__123
123123
error
Write 1123000000 in exponential form
write .0123 in exponential form
1.234e9
1.23e-2
Convert numbers from binary
Convert numbers from octal
let a = 0b11111111; // binary form of 255
let b = 0o377; // octal form of 255
alert( a == b ); // true, the same number 255 at both sides
Shorthand for hexadecinal numbers
0x
console.log(0xff);
Convert numbers to a specific base numbering system as a string
(15).toString(16)
Write a function to round number to specific number of decimal places using Math.round()
Math.round() just rounds to the nearest integer. So:
num = 1.23456
Math.round(num * 100) / 100 ; // 1.23456 -> 123.456 -> 123 -> 1.23
A function that would do it might look like:
function roundToFloat(inputNumber, numberOfDecimalPlaces) {
return Math.round(inputNumber * (10**(numberOfDecimalPlaces))) / 10**(numberOfDecimalPlaces);
}
What is returned from:
1e500
infinity
What does this return (16 nines):
9999999999999999
10000000000000000
Javascript doesn’t have integers, only 64-bit floats - and you’ve ran out of floating-point precision.
+‘Infinity’ == Infinity
true
What is returned from 1e(variable)
an error
to take the power of a variable, use **