Skip to content

Commit 524c1fe

Browse files
committed
[CON-19/st-compl] interview-with-mentor
Solving 4 tasks. Working with "var, reduce(), Set(), XOR, class". Worth noting: - all this interview (note the solutions). FS-dev: B-3 / JS basic
1 parent a30d399 commit 524c1fe

File tree

3 files changed

+122
-0
lines changed

3 files changed

+122
-0
lines changed
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
<!DOCTYPE html>
2+
<html lang="en">
3+
4+
<head>
5+
<meta charset="UTF-8">
6+
<meta name="viewport" content="width=device-width, initial-scale=1.0">
7+
<title>Interview with mentor</title>
8+
<link rel="stylesheet" href="./style.css">
9+
<script src="./interview.js" defer></script>
10+
</head>
11+
12+
<body>
13+
</body>
14+
15+
</html>
Lines changed: 106 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,106 @@
1+
'use strict';
2+
3+
// Задание 1:
4+
// Что будет выведено в alert(phrase)?
5+
6+
function sayHi() {
7+
var phrase = 'Привет!';
8+
9+
alert(phrase); // Привет!
10+
}
11+
12+
sayHi();
13+
alert(phrase); // В alert буде - undefined, а в console - ReferenceError: phrase is not defined
14+
15+
// Задание 2:
16+
// А что здесь будет в alert(phrase)?
17+
18+
if (true) {
19+
var phrase = 'Привет, ещё раз!';
20+
alert(phrase); // Привет, ещё раз!
21+
}
22+
23+
alert(phrase); // Привет, ещё раз!
24+
25+
// Задание 3:
26+
// https://bigfrontend.dev/problem/find-the-single-integer
27+
// Имея массив целых чисел, все числа появляются дважды, кроме одного, сможете ли вы быстро найти его?
28+
29+
const arr = [10, 2, 2, 1, 0, 0, 10];
30+
31+
function findSingle(arr = []) {
32+
const initSum = arr.reduce((acc, num) => acc + num, 0); // сумма 25
33+
const setSum = [...new Set(arr)].reduce((acc, num) => acc + num * 2, 0); // сумма 26
34+
35+
return setSum - initSum;
36+
}
37+
38+
// ? альтернативное решение (через побитовый оператор XOR (^))
39+
function findSingleXOR(arr = []) {
40+
return arr.reduce((acc, num) => acc ^ num, 0);
41+
}
42+
43+
console.log(findSingleXOR(arr)); // 1
44+
45+
// ! Логика XOR, побитового оператора (^):
46+
// Любое число XOR само на себя равно 0 (например, 10 ^ 10 = 0).
47+
// Любое число XOR 0 равно самому себе (например, 1 ^ 0 = 1).
48+
// Когда вы применяете reduce с оператором ^ ко всему массиву, все парные числа "схлопываются" в 0, и в конце остается только одиночное число.
49+
50+
// ? альтернативное решение (через объект)
51+
// function findSingle(arr = []) {
52+
// const obj = {};
53+
//
54+
// for (const num of arr) {
55+
// if (!obj[num]) {
56+
// obj[num] = 1;
57+
// } else {
58+
// obj[num] += 1;
59+
// }
60+
// }
61+
//
62+
// for (const key in obj) {
63+
// if (obj[key] === 1) {
64+
// return key;
65+
// }
66+
// }
67+
// }
68+
69+
console.log(findSingle(arr)); // 1
70+
71+
// Задание 4:
72+
// https://bigfrontend.dev/problem/create-a-counter-object
73+
// Создайте объект со свойством count, которое увеличивается при каждом доступе к count, начальное значение равно 0.
74+
75+
function createCounter() {
76+
class Obj {
77+
constructor() {
78+
this.counter = 0;
79+
}
80+
81+
get count() {
82+
this.counter = this.counter + 1;
83+
return this.counter;
84+
}
85+
86+
set count(value) {
87+
if (value > 10) {
88+
console.error('Prohibited');
89+
return;
90+
}
91+
92+
this.counter = value;
93+
return this.counter;
94+
}
95+
}
96+
97+
return new Obj();
98+
}
99+
100+
const counter = createCounter();
101+
console.log(counter.count); // 0, then it should increment
102+
console.log(counter.count); // 1
103+
console.log(counter.count); // 2
104+
counter.count = 100;
105+
counter.count = 9;
106+
console.log(counter.count);
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+

0 commit comments

Comments
 (0)