const nums = [1, 2, 3, 4, 5];
const result = nums
.filter(n => n > 2)
.map(n => n * 2)
.reduce((acc, n) => {
acc.push(n + 1);
return acc;
}, [])
.slice(1);
console.log(result);
console.log(nums);
Ответ: [9,11][1, 2, 3, 4, 5]
JavaScript test | #JavaScript
Please open Telegram to view this post
VIEW IN TELEGRAM
function* generator1() {
yield 1;
yield 2;
}
function* generator2() {
yield* generator1();
yield 3;
}
const gen = generator2();
console.log(gen.next().value);
console.log(gen.next().value);
console.log(gen.next().value);
Ответ:
JavaScript test | #JavaScript
Please open Telegram to view this post
VIEW IN TELEGRAM
const sym1 = Symbol('test');
const sym2 = Symbol('test');
const obj = {
[sym1]: 'value1',
[sym2]: 'value2',
regular: 'value3'
};
const sym3 = Symbol.for('global');
const sym4 = Symbol.for('global');
console.log(sym1 === sym2);
console.log(sym3 === sym4);
console.log(Object.keys(obj).length);
console.log(Object.getOwnPropertySymbols(obj).length);
Ответ:
JavaScript test | #JavaScript
Please open Telegram to view this post
VIEW IN TELEGRAM
const regex = /\d{2,}/;
console.log(regex.test('5'), regex.test('55'), regex.test('555'));Ответ:
JavaScript test | #JavaScript
Please open Telegram to view this post
VIEW IN TELEGRAM
function Person(name) {
this.name = name;
}
Person.prototype.greet = function() {
return `Hello, my name is ${this.name}`;
};
const john = new Person('John');
console.log(john.greet());Ответ:
JavaScript test | #JavaScript
Please open Telegram to view this post
VIEW IN TELEGRAM
function outer() {
console.log(innerVar);
console.log(typeof innerFunc);
var innerVar = 42;
function innerFunc() {
return innerVar;
}
let anotherVar = 100;
console.log(typeof anotherVar);
}
outer();
Ответ:
JavaScript test | #JavaScript
Please open Telegram to view this post
VIEW IN TELEGRAM
class CustomError extends Error {
constructor(message, code) {
super(message);
this.name = 'CustomError';
this.code = code;
}
}
try {
throw new CustomError('Something went wrong', 500);
} catch (error) {
console.log(error instanceof Error);
console.log(error instanceof CustomError);
console.log(error.name);
console.log(typeof error.code);
}Ответ:
JavaScript test | #JavaScript
Please open Telegram to view this post
VIEW IN TELEGRAM
const arr = [1, 2, 3, 4, 5];
const result = arr
.map(x => x * 2)
.filter(x => x > 5)
.reduce((acc, val) => {
return acc + (val % 3 === 0 ? val : 0);
}, 0);
console.log(result);
Ответ:
JavaScript test | #JavaScript
Please open Telegram to view this post
VIEW IN TELEGRAM
console.log('Start');
setTimeout(() => {
console.log('Timeout 1');
}, 0);
Promise.resolve().then(() => {
console.log('Promise 1');
}).then(() => {
console.log('Promise 2');
});
setTimeout(() => {
console.log('Timeout 2');
}, 0);
console.log('End');
Ответ:
JavaScript test | #JavaScript
Please open Telegram to view this post
VIEW IN TELEGRAM
class StateMachine {
constructor() {
this.state = 'idle';
this.transitions = {
idle: { start: 'running', reset: 'idle' },
running: { pause: 'paused', stop: 'stopped' },
paused: { resume: 'running', stop: 'stopped' },
stopped: { reset: 'idle' }
};
}
transition(action) {
const nextState = this.transitions[this.state]?.[action];
if (nextState) {
this.state = nextState;
return true;
}
return false;
}
}
const sm = new StateMachine();
console.log(sm.transition('start'));
console.log(sm.state);
console.log(sm.transition('reset'));
console.log(sm.state);Ответ:
JavaScript test | #JavaScript
Please open Telegram to view this post
VIEW IN TELEGRAM
const target = { name: 'Sarah', age: 25 };
const handler = {
get(obj, prop) {
if (prop === 'toString') {
return () => `Person: ${obj.name}`;
}
return Reflect.get(obj, prop);
},
has(obj, prop) {
return prop !== 'age' && Reflect.has(obj, prop);
}
};
const proxy = new Proxy(target, handler);
console.log(proxy.name);
console.log('age' in proxy);
console.log(proxy.toString());Ответ:
JavaScript test | #JavaScript
Please open Telegram to view this post
VIEW IN TELEGRAM
const numbers = [1, 2, 3, 4, 5, 6];
const result = numbers.reduce((acc, num) => {
if (num % 2 === 0) {
acc.even += num;
} else {
acc.odd *= num;
}
return acc;
}, { even: 1, odd: 1 });
console.log(result);
Ответ:
JavaScript test | #JavaScript
Please open Telegram to view this post
VIEW IN TELEGRAM
const matrix = [
[1, 2, 3],
[4, 5, 6],
[7, 8, 9],
];
let columnSums = [];
for (let i = 0; i < matrix[0].length; i++) {
let sum = 0;
for (let j = 0; j < matrix.length; j++) {
sum += matrix[i][i];
}
columnSums.push(sum);
}
console.log(columnSums);
Ответ:
[ 3, 15, 27 ]
Please open Telegram to view this post
VIEW IN TELEGRAM
const arr = [1, 2, 3];
const str = arr.join([4]);
console.log(typeof str);
console.log(str);
Ответ:
'14243'
JavaScript test | #JavaScript
Please open Telegram to view this post
VIEW IN TELEGRAM
var arr = Array.from({ length: 5 }, (v, i) => i * 2);
console.log(arr);
Ответ:
[0, 2, 4, 6, 8]
JavaScript test | #JavaScript
Please open Telegram to view this post
VIEW IN TELEGRAM
function main() {
console.log(1);
setTimeout(() => console.log(2), 0);
Promise.resolve().then(() => {
console.log(3);
setTimeout(() => console.log(4), 0);
}).then(() => console.log(5));
Promise.resolve().then(() => console.log(6));
console.log(7);
}
main();
Ответ:
JavaScript test | #JavaScript
Please open Telegram to view this post
VIEW IN TELEGRAM
const cache = new WeakMap();
const obj1 = { id: 1 };
const obj2 = { id: 2 };
cache.set(obj1, 'data1');
cache.set(obj2, 'data2');
obj2.newProp = 'test';
console.log(cache.has(obj1), cache.has(obj2), cache.has({ id: 1 }));
Ответ:
Please open Telegram to view this post
VIEW IN TELEGRAM
const obj = { length: 3 };
console.log(Object.keys(obj).length);
Ответ:
JavaScript test | #JavaScript
Please open Telegram to view this post
VIEW IN TELEGRAM
console.log('1');
Promise.resolve().then(() => {
console.log('2');
Promise.resolve().then(() => console.log('3'));
});
Promise.resolve().then(() => {
console.log('4');
});
setTimeout(() => console.log('5'), 0);
console.log('6');
Ответ:
JavaScript test | #JavaScript
Please open Telegram to view this post
VIEW IN TELEGRAM
var str="good wood food cat bat hat";
console.log( str.match(/((g|w|f)ood)|((c|b|h)at)/g) );
Ответ:
[ 'good', 'wood', 'food', 'cat', 'bat', 'hat' ]
JavaScript test | #JavaScript
Please open Telegram to view this post
VIEW IN TELEGRAM