JavaScript test
10.7K subscribers
3.03K photos
6 videos
4.08K links
Проверка своих знаний по языку JavaScript.

Ссылка: @Portal_v_IT

Сотрудничество: @oleginc, @tatiana_inc

Канал на бирже: telega.in/c/js_test

РКН: clck.ru/3KHeYk
Download Telegram
❗️Что будет на выходе:


const multiply = (a) => (b) => (c) => a * b * c;

const step1 = multiply(2);
const step2 = step1(3);
const result1 = step2(4);

const result2 = multiply(2)(3)(4);

const partialMultiply = multiply(5);
const result3 = partialMultiply(2)(3);

const doubler = multiply(2)(1);
const result4 = doubler(7);

console.log(result1, result2, result3, result4);


Ответ: 24, 24, 30, 14

JavaScript test | #JavaScript
Please open Telegram to view this post
VIEW IN TELEGRAM
❗️Что будет на выходе:

const firstArrayData = [ 'JavaScript', 'Universe' ];
const secondArrayData = [ 'JavaScript', 'Universe' ];

console.log(firstArrayData == secondArrayData);
console.log(firstArrayData === secondArrayData);

Ответ: false, false

JavaScript test | #JavaScript
Please open Telegram to view this post
VIEW IN TELEGRAM
❗️Что будет на выходе:

const obj = {
value: 011,
increment() {
this.value++;
}
};

const increment = obj.increment;
increment();

console.log(obj.value);

Ответ: 9

JavaScript test | #JavaScript
Please open Telegram to view this post
VIEW IN TELEGRAM
❗️Что будет на выходе:

let obj = { a: 1 };
let proto = { b: 2 };

Object.setPrototypeOf(obj, proto);

for (let key in obj) {
console.log(key);
}

Ответ: a, b

JavaScript test | #JavaScript
Please open Telegram to view this post
VIEW IN TELEGRAM
❗️Что будет на выходе:


const symbol1 = Symbol('desc1');
const symbol2 = Symbol('desc2');
const myObject = {};
myObject[symbol1] = 'Value1';
myObject[symbol2] = 'Value2';
let output = '';
for (let key in myObject) {
output += myObject[key] + ' ';
}
output += Object.getOwnPropertySymbols(myObject).length;
console.log(output);

Ответ: 2

JavaScript test | #JavaScript
Please open Telegram to view this post
VIEW IN TELEGRAM
❗️Что будет на выходе:

const cache = new Map();

function createHandler(id) {
const data = { id, value: new Array(1000).fill(id) };
cache.set(id, data);

return function(action) {
if (action === 'get') return data;
if (action === 'clear') cache.delete(id);
};
}

const handler1 = createHandler('user-1');
const handler2 = createHandler('user-2');

handler1('clear');
console.log(cache.size);
console.log(handler1('get').id);

Ответ: 1, user-1

JavaScript test | #JavaScript
Please open Telegram to view this post
VIEW IN TELEGRAM
❗️Что будет на выходе:

function createCounter() {
let count = 0;

const counter = {
increment() {
count++;
return count;
},
getCount() {
return count;
}
};

return counter;
}

let c1 = createCounter();
c1.increment();
c1.increment();

let c2 = c1;
c1 = null;

console.log(c2.getCount());

Ответ: 2

JavaScript test | #JavaScript
Please open Telegram to view this post
VIEW IN TELEGRAM
❗️Что будет на выходе:

const obj = { a: 1, b: 2, c: 3 };
const result = Object.values(obj).reduce((acc, curr) => acc * curr, 1);
console.log(result);

Ответ: 6

JavaScript test | #JavaScript
Please open Telegram to view this post
VIEW IN TELEGRAM
❗️Что будет на выходе:

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);

Ответ: 1, 2, 3

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);

Ответ: false true 1 2

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'));

Ответ: false, true, true

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());


Ответ: Hello, my name is John

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();


Ответ: undefined 'function' 'number'

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);
}


Ответ: true, true, CustomError, number

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);

Ответ: 6

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');

Ответ: Start, End, Promise 1, Promise 2, Timeout 1, Timeout 2

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);


Ответ: true running false running

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());


Ответ: Sarah false Person: Sarah

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);

Ответ: { even: 13, odd: 15 }

JavaScript test | #JavaScript
Please open Telegram to view this post
VIEW IN TELEGRAM