**UA** Для того, щоб правильно відповісти на це питання необхідно розуміти як працювати з оператором [switch](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/switch), як передаються параметри функції а також як працюють функція [isNaN](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/isNaN) та оператор або (`||`). Спочатку варто зрозуміти яке ж значення буде в параметрі `x`. Оскільки при виклику функції не було передано жодних параметрів то буде використано [значення за замовчуванням](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Functions#default_parameters) - `undefined`. Це можна легко перевірити виконавши наступний шматок коду. ```javascript= const f = (arg) => console.log(arg === undefined); f(); ``` Далі, варто розуміти як працює оператор `switch`. Цей оператор оцінює значення яке йому надається, та виконує код пов'язаний з відповідним `case` та, що важливо пам'ятати, з усіма наступними конструкціями. У цьому можна легко переконатись виконавши наступний код: ```javascript= switch(2) { case 1: console.log('1'); case 2: console.log('2'); case 3: console.log('3'); default: console.log('default') } ``` Таким чином у консоль буде виведено: ``` 2 3 default ``` У javascript є 3 механізми, які зупиняють виконання усіх `case` конструкцій, це: `break`, `return`, `throw`. * `break` - зупинить виконання усіх наступних `case` конструкцій, і продовжить виконання функції. * `return` - зупинить виконання усіх наступних `case` конструкцій, і поверне значення з функції. * `throw` - зупинить виконання усіх `case` конструкцій і кине помилку. Так, якшо ми перепишемо код вище з використанням `break` ми отримаємо зовсім інший результат: ```javascript= switch(2) { case 1: console.log('1'); break; case 2: console.log('2'); break; case 3: console.log('3'); break; default: console.log('default') } ``` Буде виведено тільки одна цифра: ``` 2 ``` Наступним важливим кроком для розв'язання задачі має бути визначення значення, яке поверне функція `isNaN`. Щоб це зрозуміти спершу необхідно розібратись, що таке `NaN`. NaN - пердставляє значення яке не вдалось перетворити у число (Not-a-Number), такі випадки можуть траплятись коли: * Конвертується значення, яке не може бути презентовано числом, у число (`Number('foo')`) * Виконується матемтична операція результатом якої не є реальне число (`Math.sqrt(-1)`) * Оператором математичної операції є NaN (`7 * NaN`) * Результат виразу є невизначеним (`0 * Infinity`) * Будь-яка операція, яка включає строку і не є операцією додавання (`'foo' / 5`) Функція `isNaN` конвертує передане в неї значення у число і перевіряє чи це значення є `NaN`. ```javascript= console.log(isNaN('55')); // false console.log(isNaN(undefined)); // true ``` Останнє, що потрібно знати для розв'язання цієї задачі це поведінку оператора або - `||`. Цей оператор повертає друге значення лише тоді коли переше - [falsy](https://developer.mozilla.org/en-US/docs/Glossary/Falsy). ```javascript= console.log(false || 'shown'); // 'shown' console.log(true || 'not shown'); // true ``` Повертаючись до задачі, ми можемо сказати що у функції параметр `x` має значення `undefined`. Оскільки це значення не може бути конвертованим у число, то функція `isNaN` поверне `true`. Отже оператор `||` теж поверне `true` оскільки це значення є правдивим. А це означає, що виконається констркуція ```javascript= case true: { console.log(1); } ``` а оскільки немає жодного оператора який би зупиняв виконання `case` конструкцій, то також виконається і `default`(Хоча він нічого не виводить у даному завданні). Отже у консоль буде виведено: ``` 1 ``` Правильна відповідь - 1. --- **EN** In order to answer this question correctly, we need to understand how to work with the [switch operator](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/switch), how function parameters are passed as well as how the [isNaN](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/isNaN) function and the OR operator (`||`) work. Firstly, it is necessary to understand what value will be in the parameter `x`. Since no parameters were passed to the function when it was called, the [default value](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Functions#default_parameters) - `undefined` will be used. This can be easily checked by following piece of code. ```javascript= const f = (arg) => console.log(arg === undefined); f(); ``` Next, we should understand how the `switch` operator works. This operator evaluates the value given to it and executes the code associated with the corresponding `case` clause and, what is important to remember, all the following statements will be executed as well. This can be easily checked by executing the following code: ```javascript= switch(2) { case 1: console.log('1'); case 2: console.log('2'); case 3: console.log('3'); default: console.log('default') } ``` The result in console will be: ``` 2 3 default ``` There are 3 ways to stop the execution of all `case` clauses - `break`, `return`,` throw`. * `break` - stop execution of all subsequent `case` clauses, and continue execution of function. * `return` - stop execution of all following `case` clauses, and return value from function. * `throw` - stop execution of all `case` clauses and throw an error. So, if we rewrite the code above using `break` we will get a completely different result: ```javascript= switch(2) { case 1: console.log('1'); break; case 2: console.log('2'); break; case 3: console.log('3'); break; default: console.log('default') } ``` Result in console: ``` 2 ``` On the next step we should determine the value that the `isNaN` function will return. To understand this, we must first understand what `NaN` is. NaN - represents a value that could not be converted into a number (Not-a-Number), such cases may occur when: * Number cannot be parsed (`Number('foo')`) * A mathematical operation is performed that does not result in a real number (`Math.sqrt (-1)`) * The operator of the mathematical operation is NaN (`7 * NaN`) * The result of the expression is indefinite (`0 * Infinity`) * Any operation that includes a string and is not an add operation (`'foo'/ 5`) The `isNaN` function converts the value passed to it into a number and checks if this value is` NaN`. ```javascript= console.log(isNaN('55')); // false console.log(isNaN(undefined)); // true ``` The last thing we need to know to solve this problem is the behavior of the operator OR (`||`). This statement returns the second value only when the first is [falsy](https://developer.mozilla.org/en-US/docs/Glossary/Falsy). ```javascript= console.log(false || 'shown'); // 'shown' console.log(true || 'not shown'); // true ``` Now, we can say that in the function the parameter `x` has the value `undefined`. The `isNaN` function will return `true`, because this value cannot be converted to a number. So, the `||` operator will also return `true` because this value is truly. This means that the following clause is executed ```javascript = case true: { console.log (1); } ``` and since there is no operator that would stop the execution of `case` clause, the `default` is also executed (although it does not output anything in this task). So the console will display: ``` 1 ``` The correct answer is - 1.