From 25th Jan to 27th Jan, Wantedly took part in JSConf.Asia 2018, where JavaScript developers gather for a 3-day event with two main objectives:
- Push the boundaries of what is thought to be conceivable with JS
- Engage in exceptional human social activities that encourage community and friendship building
We distributed a short but tricky JS quiz during this event, and the enthusiasm showed by participants was much higher than expected. Over 100 JS developers attempted the quiz! Thank you everyone for your kind support :)
Here are the questions:
What are the result of the following expressions?
! ! [ ] // -> _______
[ ] == true // -> _______
Number.MIN_VALUE > 0 // -> _______
(0.1 + 0.2) === 0.3 // -> _______
[1, 2, 3] + [4, 5, 6] // -> _______
'b' + 'a' + + 'a' + 'a' // -> _______ (Hint: Minions ;) )
What will the following code output?
const arr = [10, 12, 15, 21];
for (var i = 0; i < arr.length; i++) {
setTimeout(function() {
console.log('Index: ' + i + ', element: ' + arr[i]);
}, 3000);
}
_____________________________
What do the two functions below return?
function func1() {
return { bar: "hello" }
}
function func2() {
return
{ bar: "hello" }
}
func1() // -> ____________
func2() // -> ____________
Name one programming paradigm that characterizes JavaScript.
______________________________
-----------------------------------------------------------------------------------------------------------------------------
It was an intense fight to be the fastest and most accurate JS developer at JSConf Asia 2018. Before we announce the winners, here are the answers and explanations.
! ! [ ] // -> true
An array is a truthy value. By using the logical NOT operator (!) twice, the empty array is converted first to false, then true.
- Description of logical NOT operator:
https://www.ecma-international.org/ecma-262/#sec-logical-not-operator
[ ] == true // -> false
An array is a truthy value, however, it's not equal to true.
- Description of empty array:
https://stackoverflow.com/questions/5491605/empty-arrays-seem-to-equal-true-and-false-at-the-same-time
- Description of Abstract Equality Comparison (==):
https://www.ecma-international.org/ecma-262/#sec-abstract-equality-comparison
Number.MIN_VALUE > 0 // -> true
The value of Number.MIN_VALUE is the smallest positive value of the Number type, which is approximately 5 × 10-324.
In the IEEE 754-2008 double precision binary representation, the smallest possible value is a denormalized number. If an implementation does not support denormalized values, the value of Number.MIN_VALUE must be the smallest non-zero positive value that can actually be represented by the implementation.
(0.1 + 0.2) === 0.3 // -> false
The constants 0.2 and 0.3 in JavaScript are approximations to their true values. It happens that the closest double to 0.2 is larger than the rational number 0.2 but that the closest double to 0.3 is smaller than the rational number 0.3. The sum of 0.1 and 0.2 ends up being larger than the rational number 0.3 and produces the result "false".
This problem is so known that there is even a website called 0.30000000000000004.com. It occurs in every language that uses floating-point math, not just JavaScript :D
[1, 2, 3] + [4, 5, 6] // -> 1, 2, 34, 5, 6
[1, 2, 3] + [4, 5, 6]
// call toString()
[1, 2, 3].toString() + [4, 5, 6].toString()
// concatenation
'1,2,3' + '4,5,6'
// ->
'1,2,34,5,6'
'b' + 'a' + + 'a' + 'a' // -> baNaNa (Hint: Minions ;) )
The expression is evaluated as 'b' + 'a' + (+'a') + 'a', which converts 'a' to not a number.
What will the following code output?
const arr = [10, 12, 15, 21];
for (var i = 0; i < arr.length; i++) {
setTimeout(function() {
console.log('Index: ' + i + ', element: ' + arr[i]);
}, 3000);
}
Index: 4, value: undefined (printed 4 times)
Full explanation here:
https://www.reddit.com/r/javascript/comments/7535tm/amazon_web_developer_loop_timeout_interview/
What do the two functions below return?
function func1() {
return { bar: "hello" }
}
function func2() {
return
{ bar: "hello" }
}
func1() // -> { bar: "hello" }
func2() // -> undefined
The reason for this has to do with the fact that semicolons are technically optional in JavaScript (although omitting them is generally really bad form).
As a result, the func2 is interpreted as:
function func2() {
return;
{ bar: "hello" }; // never be executed
}
Name one programming paradigm that characterizes JavaScript.
We accepted all answers that made sense for this question :)
And now, finally, for our winners!
1. Paul Clark
Score: 9 Time: 2:18
2. Thai Pangsakulyanont
Score: 8 Time: 2:14
3. Rama
Score: 7 Time: 2:20
Special Mention:
4. Nick Peekhanov
Score: 7 Time: 3:21
5. Aivan Monceller
Score: 6 Time: 2:03