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
❗️Что будет на выходе:

function greet(name) {
return `Hello, ${name}!`;
}

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

const user = 'Sarah';
const status = 'online';

console.log(highlight`User ${user} is currently ${status}.`);

...Ответ: User <em>Sarah</em> is currently <em>online</em>.

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

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
Please open Telegram to view this post
VIEW IN TELEGRAM
❗️Что будет на выходе:

const target = { name: 'Maya', age: 25 };
const handler = {
get(obj, prop) {
if (prop in obj) {
return obj[prop];
}
return `Property '${prop}' not found`;
},
set(obj, prop, value) {
if (typeof value === 'string') {
obj[prop] = value.toUpperCase();
} else {
obj[prop] = value;
}
return true;
}
};
const proxy = new Proxy(target, handler);
proxy.city = 'tokyo';
console.log(proxy.name);
console.log(proxy.city);
console.log(proxy.country);

Ответ: Maya TOKYO Property 'country' not found

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

const result = (function() {
let count = 0;

return {
increment() {
return ++count;
},
get value() {
return count;
},
reset() {
const oldCount = count;
count = 0;
return oldCount;
}
};
})();

result.increment();
result.increment();
console.log(result.reset() + result.value + result.increment());

Ответ: 3

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

const user = {
profile: {
settings: {
theme: 'dark'
}
}
};

const getTheme = (obj) => obj?.profile?.settings?.theme ?? 'light';
const getLanguage = (obj) => obj?.profile?.settings?.language ?? 'en';
const getNotifications = (obj) => obj?.profile?.notifications?.enabled ?? true;

console.log(getTheme(user));
console.log(getLanguage(user));
console.log(getNotifications(user));
console.log(getTheme(null));

Ответ: dark, en, true, light
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 weakMap = new WeakMap();
const array = [{}, {}];

array.forEach(obj => weakMap.set(obj, obj));
const result = array.map(obj => weakMap.get(obj) === obj);

console.log(result);

Ответ: [true, true]

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

const team = {
members: ['Alice', 'Bob', 'Charlie'],
[Symbol.iterator]: function*() {
let index = 0;
while(index < this.members.length) {
yield this.members[index++].toUpperCase();
}
}
};

const result = [];
for (const member of team) {
result.push(member);
}

console.log(result.join('-'));

Ответ: ALICE-BOB-CHARLIE

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

var str="My name is John";
var words1=str.split(" ",3);
console.log("words1:",words1);
var words2=str.split(" ",5);
console.log("words2:",words2);


Ответ:
words1:[ 'My', 'name', 'is' ]
words2:[ 'My', 'name', 'is', 'John' ]


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

function createCounter() {
let count = 0;
return {
increment: () => ++count,
decrement: () => --count,
getValue: () => count
};
}

const counter1 = createCounter();
const counter2 = createCounter();
counter1.increment();
counter1.increment();
counter2.increment();
console.log(counter1.getValue(), counter2.getValue());
counter1.decrement();
console.log(counter1.getValue(), counter2.getValue());

Ответ: 2 1 1 1

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' };
const obj3 = obj1;

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

console.log(wm.get(obj1));
console.log(wm.get(obj2));
console.log(wm.get(obj3));
console.log(wm.has(obj1));
console.log(wm.size);

Ответ: value3 value2 value3 true undefined

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

const user = {
profile: {
name: 'Alice',
settings: {
notifications: {
email: true,
sms: false
}
}
},
getPreference(type) {
return this.profile?.settings?.notifications?.[type] ?? 'not configured';
}
};

const admin = {
profile: {
name: 'Admin',
settings: null
},
getPreference: user.getPreference
};

console.log(admin.getPreference('email'));

Ответ: not configured

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

class CustomError extends Error {
constructor(message) {
super(message);
this.name = 'CustomError';
}
}

try {
throw new CustomError('Something went wrong');
} catch (e) {
console.log(e instanceof Error);
console.log(e instanceof CustomError);
console.log(e.constructor.name);
console.log(e.name);
}

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

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

const regex = /a/g;
const str = 'banana';
console.log(str.match(regex));

Ответ: ['a', 'a']

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 obj = {};
let value = 0;

Object.defineProperty(obj, 'prop', {
get() {
return value;
},
set(newValue) {
value = newValue + 1;
},
configurable: true,
enumerable: true
});

obj.prop = 10;
console.log(obj.prop);

Ответ: 11

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

const a = async () => {
return Promise.reject('rejected');
};

a().catch(error => console.log(error));
console.log('done');

Ответ: 'done', 'rejected'

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

const obj = { count: 0 };
const arr = [obj, obj, obj];

function increment(item) {
item.count++;
return item;
}

const results = arr.map(increment);
console.log(obj.count);
console.log(results[0] === results[1]);
console.log(results.length);
console.log(arr[0].count);

Ответ: 3 true 3 3

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

function createCounter() {
let count = 0;

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

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

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

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

const { increment, reset } = counter;
increment();
reset();
increment();

console.log(counter.increment());

Ответ: 2

JavaScript test | #JavaScript
Please open Telegram to view this post
VIEW IN 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;

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'));
setTimeout(() => console.log('4'), 0);
console.log('5');
Promise.resolve().then(() => {
console.log('6');
return Promise.resolve();
}).then(() => console.log('7'));
queueMicrotask(() => console.log('8'));
console.log('9');

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

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