JavaScript test
10.9K subscribers
3.02K photos
5 videos
4.01K links
Проверка своих знаний по языку JavaScript.

Ссылка: @Portal_v_IT

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

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

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

const map = new WeakMap();

let obj1 = { name: 'user1' };
let obj2 = { name: 'user2' };

map.set(obj1, 'data for user1');
map.set(obj2, 'data for user2');

console.log(map.has(obj1));
obj1 = null;

// After garbage collection runs
console.log(map.has(obj1));
console.log(map.get(obj2));

Ответ: true, false, data, for user2

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

console.log(1);
setTimeout(() => console.log(2), 0);
Promise.resolve()
.then(() => {
console.log(3);
return Promise.resolve(4);
})
.then(console.log);
console.log(5);

Ответ: 1, 5, 3, 4, 2

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

function asyncQuiz() {
return new Promise((resolve) => {
setTimeout(() => resolve('Hello'), 1000);
});
}

async function runAsyncQuiz() {
const result = await asyncQuiz();
console.log(result);
}

runAsyncQuiz();
console.log('World');

Ответ: World Hello

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

var arr=[1,2,3,4,5];
console.log(arr.map((prev,curr)=>prev+curr));
console.log(arr.reduce((a,b)=>a+b));
console.log(arr.filter((a,b)=> (a + b) <= 5));

Ответ: [ 1, 3, 5, 7, 9 ]
15
[ 1, 2, 3 ]


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

const users = [
{ id: 1, name: 'Alice' },
{ id: 2, name: 'Bob' },
{ id: 3, name: 'Charlie' }
];

const userScores = new WeakMap();

// Set scores for users
userScores.set(users[0], 95);
userScores.set(users[1], 80);

// Remove reference to Bob
users[1] = null;

let sum = 0;
for (const user of users) {
if (user && userScores.has(user)) {
sum += userScores.get(user);
}
}

console.log(sum);

Ответ: 95

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

const wm = new WeakMap();
const obj1 = { name: 'first' };
const obj2 = { name: 'second' };

wm.set(obj1, 'value1');
wm.set(obj2, 'value2');

const keys = [];
for (let key of wm) {
keys.push(key);
}

console.log(keys.length);
console.log(wm.has(obj1));
console.log(wm.get(obj2));

console.log(sum);

Ответ: TypeError: vm is noy iterable

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

class DataProcessor {
constructor(value) {
this.value = value;
}

transform(fn) {
return new DataProcessor(fn(this.value));
}

getValue() {
return this.value;
}
}

const multiply = x => x * 2;
const add = x => x + 10;
const square = x => x * x;

const result = new DataProcessor(5)
.transform(multiply)
.transform(add)
.transform(square)
.getValue();

console.log(result);

Ответ: 400

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 === 'info') {
return `${obj.name} is ${obj.age}`;
}
return Reflect.get(obj, prop);
},
has(obj, prop) {
return prop !== 'age' && Reflect.has(obj, prop);
}
};
const proxy = new Proxy(target, handler);
console.log(proxy.info);
console.log('age' in proxy);
console.log('name' in proxy);


Ответ: Sarah is 25, false, true

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

const products = [
{ id: 1, name: 'Laptop', price: 1200, category: 'Electronics' },
{ id: 2, name: 'Headphones', price: 100, category: 'Electronics' },
{ id: 3, name: 'Book', price: 15, category: 'Books' },
{ id: 4, name: 'Shirt', price: 25, category: 'Clothing' },
{ id: 5, name: 'Coffee Mug', price: 10, category: 'Kitchen' }
];

const result = products
.filter(p => p.price > 20)
.map(p => ({ name: p.name, value: p.price * 0.9 }))
.reduce((acc, item) => {
acc.names.push(item.name);
acc.total += item.value;
return acc;
}, { names: [], total: 0 });

console.log(result);

Ответ: { names: ['Laptop', 'Headphones', 'Shirt'], total: 1192.5 }

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

console.log('1');

setTimeout(() => console.log('2'), 0);

Promise.resolve().then(() => console.log('3'));

queueMicrotask(() => console.log('4'));

setTimeout(() => {
console.log('5');
Promise.resolve().then(() => console.log('6'));
}, 0);

console.log('7');

Ответ: 1 7 3 4 2 5 6

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