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

Ссылка: @Portal_v_IT

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

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

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

const cache = new WeakMap();

const user1 = { name: 'Alice' };
const user2 = { name: 'Bob' };

cache.set(user1, { lastLogin: 'yesterday' });
cache.set(user2, { lastLogin: 'today' });

const result = [];
result.push(cache.has(user1));
result.push(cache.get(user2).lastLogin);

let user3 = { name: 'Charlie' };
cache.set(user3, { lastLogin: 'now' });
result.push(cache.has(user3));

user3 = null; // Removing the reference
// Garbage collector might run here in real situations

const user4 = { name: 'Charlie' }; // Same name, different object
result.push(cache.has(user4));

console.log(result);

Ответ: [true, 'today', true, false]


JavaScript test | #JavaScript & Max
❗️Что будет на выходе:

async function foo() {
console.log('Start');
await Promise.resolve().then(() => {
console.log('Inside Promise');
});
console.log('End');
}

foo();
console.log('Outside');

Ответ: Start, Outside, Inside, Promise, End


JavaScript test | #JavaScript & Max
❗️Что будет на выходе:

let x = 5;
let result = typeof (x + "10");

console.log(result);

Ответ: string

JavaScript test | #JavaScript & Max
❗️Что будет на выходе:

const obj = { name: 'Alice', age: 30 };
const handler = {
get(target, prop) {
return prop in target ? target[prop] : `Property '${prop}' doesn't exist`;
}
};

const proxy = new Proxy(obj, handler);

const descriptors = Object.getOwnPropertyDescriptors(obj);
Reflect.defineProperty(obj, 'city', {
value: 'New York',
enumerable: false
});

console.log(proxy.city, proxy.country);

Ответ: New York Property 'country' doesn't exist

JavaScript test | #JavaScript & Max
❗️Что будет на выходе:

const numbers = [0,0,0];

let i = 0;
const result = numbers.reduce((acc, _) => {
return ++acc;
}, i);

console.log(i, result);

Ответ: 0 3


JavaScript test | #JavaScript & Max
❗️Что будет на выходе:

const array = [1, 2, 3];
array[-1] = 0;

console.log(array.length);

Ответ: 3

JavaScript test | #JavaScript & Max
❗️Что будет на выходе:

const numbers = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10];

const result = numbers
.filter(n => n % 2 === 0)
.map(n => n * 2)
.reduce((acc, curr, idx, arr) => {
if (idx === arr.length - 1) {
return (acc + curr) / arr.length;
}
return acc + curr;
}, 0);

console.log(result);

Ответ: 12

JavaScript test | #JavaScript & Max
❗️Что будет на выходе:

function createCounter() {
let count = 0;

function increment() {
count += 1;
return count;
}

function decrement() {
count -= 1;
return count;
}

return { increment, decrement, value: () => count };
}

const counter = createCounter();
counter.increment();
counter.increment();
counter.decrement();

console.log(counter.value() + counter.increment());

Ответ: 3

JavaScript test | #JavaScript
❗️Что будет на выходе:

const obj = {
x: 10,
foo() {
setTimeout(function() {
console.log(this.x);
}, 1000);
}
};
obj.foo();

Ответ: undefined


JavaScript test | #JavaScript & Max
❗️Что будет на выходе:

class ShoppingCart {
constructor() {
if (ShoppingCart.instance) {
return ShoppingCart.instance;
}

this.items = [];
ShoppingCart.instance = this;
}

addItem(item) {
this.items.push(item);
}

getItems() {
return [...this.items];
}
}

const cart1 = new ShoppingCart();
const cart2 = new ShoppingCart();

cart1.addItem('Book');
cart2.addItem('Laptop');

console.log(cart1.getItems());

Ответ: ['Book', 'Laptop']

JavaScript test | #JavaScript & Max