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