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

Ссылка: @Portal_v_IT

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

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

РКН: clck.ru/3KHeYk
Download Telegram
❗️Что будет на выходе:
let x = 5;
let result = typeof (x + "10");

console.log(result);


Ответ: string

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

function createCounter() {
let count = 0;

return {
increment() {
count++;
return count;
},
decrement() {
count--;
return count;
},
getValue() {
return count;
}
};
}

const counter1 = createCounter();
const counter2 = createCounter();

counter1.increment();
counter1.increment();
counter2.increment();
counter1.decrement();

console.log(counter1.getValue() + counter2.getValue());

Ответ: 2


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

function highlight(strings, ...values) {
return strings.reduce((result, str, i) => {
const value = values[i] ? `<span>${values[i]}</span>` : '';
return result + str + value;
}, '');
}

const language = 'JavaScript';
const years = 10;

const result = highlight`I have been coding in ${language} for ${years} years`;
console.log(result);

Ответ: I have been coding in <span>JavaScript</span> for <span>10</span> years


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

setTimeout(() => console.log('Timeout 1'), 100);

setTimeout(() => {
console.log('Timeout 2');
Promise.resolve().then(() => console.log('Promise in Timeout 2'));
}, 50);

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

setTimeout(() => console.log('Timeout 3'), 150);

console.log('Sync');

Ответ: Sync, Promise 1, Timeout 2, Promise in Timeout 2, Timeout 1, Timeout 3


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

const memoryLeak = () => {
const cache = new Map();
const weakCache = new WeakMap();

const objKey = { id: 123 };
const data = { name: 'User data', value: 42 };

cache.set(objKey, data);
weakCache.set(objKey, data);

// Simulate removing reference to key
const result = { map: cache.has(objKey), weakMap: weakCache.has(objKey) };
// objKey = null; // This would be an error, as const can't be reassigned

return result;
};

console.log(memoryLeak());

Ответ: { map: true, weakMap: true }

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

function processConfig(config) {
const settings = {
timeout: config.timeout ?? 1000,
retries: config.retries ?? 3,
logging: config.logging ?? false,
debug: config.debug || true
};

return settings;
}

const userConfig = {
timeout: 0,
retries: null,
logging: false,
debug: false
};

console.log(processConfig(userConfig));

Ответ: { timeout: 0, retries: 3, logging: false, debug: true }

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

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