📜 ⬆️ ⬇️

Analysis of the final of the quiz competition at the hh.ru stand at # HolyJS18

Hello, this is the last part of the analysis of issues from our booth.


Questions on React here .


Analysis of the first four rounds here .



Here questions on the topics are practically not grouped, we sort everything one by one.


Go:


What will be the result?

let i = 0; let a = 1; for (; a >= 0; i++) { a -= 0.1; } console.log(i); a) 1 b) 11 c)  d) 10 

Answer + parse

b) 11
It's all quite simple. This is more a question of attention. The cycle will be executed 11 times, because we are not strict
greater than > , and greater than or equal to >= .


What will be the result?

 {} + []; a) 0 b) null c) '[object Object]' d) NaN 

Answer + parse

a) 0
Here the trick is that {} will be interpreted not as an object, but as an empty block of code. Therefore, the expression is converted to +[] , and this is 0 , since + before the value converts it to a number. I would like to note that if the code is written as console.log({} + []); , it will be already [object Object] , since here {} will be interpreted as an object.


What will be the result?

 [] + {}; 

a) 0
b) null
c) '[object Object]'
d) NaN


Answer + parse

c) '[object Object]'
According to the rules, both operands are brought to the string at the empty array - this is the empty string, and at the object [object Object]


What will this code output?

 let response = { data: '', errors: { code: 403 } }; console.log(typeof response.data.link); a) undefined b) object c) string d) function 

Answer + parse

d) function
The prototype string has a link method, so the correct answer is function


What will this code output?

 const button = document.querySelector('button'); button.addEventListener('click', () => { console.log('FUS'); }); button.addEventListener('click', () => Promise.resolve('RO').then((x) => console.log(x))); button.addEventListener('click', () => { console.log('DAH!'); }); button.click(); a) FUS RO DAH! b) FUS DAH! RO c) RO FUS DAH! d) DAH! RO FUS 

Answer + parse

b) FUS DAH! RO
We here trigger click programmatically, and it gets into the call stack, the first handler is triggered - we FUS , we get into the second - we add to the RO queue, since click still in the stack - do not cause promis. We fall into the following handler - we derive DAH! . click worked, the queue is empty, the promise is triggered.


What will this code display when clicking on a button?

 const button = document.querySelector('button'); button.addEventListener('click', () => { console.log('FUS'); }); button.addEventListener('click', () => Promise.resolve('RO').then((x) => console.log(x))); button.addEventListener('click', () => { console.log('DAH!'); }); button.click(); a) FUS RO DAH! b) FUS DAH! RO c) RO FUS DAH! d) DAH! RO FUS 

Answer + parse

a) FUS RO DAH!
Here the user presses a button, so there is no click in the stack, and in the second handler, the promise will work immediately.


What will this code output?

 console.log(typeof Function````); a) TypeError b) SyntaxError c) 'function' d) 'undefined' 

Answer + parse

d) 'undefined'
When using template literals after the function name, it is called with an array, where there is a passed value and an array with a raw value. Function , the same as new Function is a function constructor. When an empty string is passed to it, we will get a function of the form (){} , respectively, no matter what argument we call it, it will return undefined , here it is called with an array - ['', raw: ['']] . This is about the same as writing Function('')('')


What will this code output?

 function f(a, b, c) { 'use strict'; return f.length; } console.log(f(100, 2)); a) undefined b) 2 c) 3 d)  

Answer + parse

c) 3
Exhaustive description with MDN :
The length property is a property of the function object and indicates how many arguments the function expects, that is, the number of formal parameters. This quantity does not include residual parameters.


What will be the result?

 const dict = {}; dict[[1]] = 2; dict[dict] = 3; dict[1 / 0] = 4; a) {'1': 2, '[object Object]': 3, 'Infinity': 4} b) Assignment to constant variable c) {'[1]': 2, 'dict': 3, '1/0': 4} d) {'1': 4, '[object Object]': 3} 

Answer + parse

a) {'1': 2, '[object Object]': 3, 'Infinity': 4}
The error Assignment to constant variable will not be here. const does not allow you to change the link, and you can modify the object. All object keys must be strings, so for each of them toString is called. The array is cast to 1 , the object to [object Object] , and when divided by zero, we get Infinity .


What will this code output?

 console.log(!![] > [], ![] == []); a) false false b) false true c) true true d) true false 

Answer + parse

c) true true
Comparisons > >= <= < - cause an arithmetic comparison, first we cast the array to a boolean value and get true, and then the comparison is 1> 0, which obviously gives true. In the second part of the question, we cast the array to false , then false is false to 0 , and the array is reduced to a primitive, it turns out to be an empty string '' , and they are equal.


This concludes our analysis, thank you for your attention!


')

Source: https://habr.com/ru/post/431744/


All Articles