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
❗️Что будет на выходе:
class Logger {
constructor(prefix) {
this.prefix = prefix;
}
log(message) {
console.log(`${this.prefix}: ${message}`);
}
}

class Database {
constructor(logger) {
this.logger = logger;
}
save(data) {
this.logger.log(`Saving ${data}`);
return `${data}_saved`;
}
}

const logger = new Logger('DB');
const db = new Database(logger);
const result = db.save('user');
console.log(result);

Ответ: DB: Saving user user_saved

JavaScript test | #JavaScript
❗️Что будет на выходе:
const str = "a 1 b 2 c 3 d 04";

function foo(str) {
const regex = /\d+/g;
const matches = str.match(regex);
return matches.lenght;
}


Ответ: 4

JavaScript test | #JavaScript
❗️Что будет на выходе:
const obj = { a: 1, b: 2 };
const proxy = new Proxy(obj, {
get(target, prop) {
return prop in target ? target[prop] : 0;
}
});

console.log(proxy.a, proxy.b, proxy.c);


Ответ: 1, 2, 0

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 in obj) {
return obj[prop];
}
return `Property '${prop}' not found`;
},
set(obj, prop, value) {
obj[prop] = value.toString().toUpperCase();
return true;
}
};

const proxy = new Proxy(target, handler);
proxy.city = 'boston';
console.log(proxy.name);
console.log(proxy.city);
console.log(proxy.country);


Ответ: Sarah BOSTON Property 'country' not found

JavaScript test | #JavaScript
Please open Telegram to view this post
VIEW IN TELEGRAM
❗️Что будет на выходе:
class Rectangle {
constructor(width, height) {
this.width = width;
this.height = height;
}

get area() {
return this.width * this.height;
}
}

const rectangle = new Rectangle(5, 10);
console.log(rectangle.area());


Ответ: TypeError: rectangle, area is not a function at line 13

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

var a=[1,2,3,4,5];
a.every(x=>{
console.log(x);
return x<4;
})


Ответ:
1
2
3
4


JavaScript test | #JavaScript
Please open Telegram to view this post
VIEW IN TELEGRAM
❗️Что будет на выходе:
const numbers = [1, 2, 3];
numbers.push(4);
console.log(numbers);

Ответ: [1, 2, 3, 4]

JavaScript test | #JavaScript
Please open Telegram to view this post
VIEW IN TELEGRAM
❗️Что будет на выходе:
class Counter {
constructor(max) {
this.max = max;
}

*[Symbol.iterator]() {
let current = 0;
while (current < this.max) {
yield current++;
}
}
}

const counter = new Counter(3);
const result = [...counter, ...counter];
console.log(result);

Ответ: [0, 1, 2, 0, 1, 2]

JavaScript test | #JavaScript
Please open Telegram to view this post
VIEW IN TELEGRAM
❗️Что будет на выходе:
const x = [1, 2, 3, 4, 5];
console.log(x.slice(1, 3));

Ответ: [2, 3]

JavaScript test | #JavaScript
Please open Telegram to view this post
VIEW IN TELEGRAM
❗️Что будет на выходе:
const person = {
name: "John",
greet: function() {
const getMessage = () => `Hello, ${this.name}`;
return getMessage();
}
};

console.log(person.greet());

Ответ: Hello, John

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 arr = Array.from({ length: 3 }, (_, i) => i * 2);

for (const i of arr) {
console.log(i);
}

Ответ: 0, 2, 4

JavaScript test | #JavaScript
Please open Telegram to view this post
VIEW IN TELEGRAM
❗️Что будет на выходе:
const numbers = [1, 2, 3, 4, 5];

const result = numbers
.map(n => n * 2)
.filter(n => n > 5)
.reduce((acc, n, index) => {
acc.sum += n;
acc.indices.push(index);
return acc;
}, { sum: 0, indices: [] });

console.log(result.sum);
console.log(result.indices);

Ответ: 18 [0, 1, 2]

JavaScript test | #JavaScript
Please open Telegram to view this post
VIEW IN TELEGRAM
❗️Что будет на выходе:
const array = Array.from({ length: 5 }, () => Math.random() > 0.5);

console.log(array);


Ответ: An array of 5 random true/false values

JavaScript test | #JavaScript
Please open Telegram to view this post
VIEW IN TELEGRAM
❗️Что будет на выходе:
const array = [1, 2, 3, 4, 5];
const [first, ...rest] = array;

const modified = rest.map((n, i) => i % 2 === 0 ? n * 2 : n);

console.log(first, modified);


Ответ: 1, [4, 3, 8, 5]

JavaScript test | #JavaScript
Please open Telegram to view this post
VIEW IN TELEGRAM
❗️Что будет на выходе:
function processData(data) {
try {
if (!data) {
throw new TypeError('Data is missing');
}

const result = data.process();
return result;
} catch (error) {
console.log(error instanceof ReferenceError ? 1 :
error instanceof TypeError ? 2 :
error instanceof SyntaxError ? 3 : 4);
}
}

processData(null);


Ответ: 2

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

const add = x => y => x + y;
const multiply = x => y => x * y;
const subtract = x => y => x - y;

const result = add(10)(multiply(2)(subtract(5)(3)));
console.log(result);


Ответ: 14

JavaScript test | #JavaScript
Please open Telegram to view this post
VIEW IN TELEGRAM
❗️Что будет на выходе:
setTimeout(() => {
console.log('setTimeout 1');
Promise.resolve().then(() => console.log('Promise 1'));
}, 0);

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

console.log('Sync');

Ответ: Sync, Promise 2, setTimeout 1, Promise 1, setTimeout 2

JavaScript test | #JavaScript
Please open Telegram to view this post
VIEW IN TELEGRAM
❗️Что будет на выходе:
const person = {
name: "John",
greet: function() {
const getMessage = () => `Hello, ${this.name}`;
return getMessage();
}
};

console.log(person.greet());

Ответ: Generic sound

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 obj = {
name: 'Sarah',
getName() {
return this.name;
},
getNameArrow: () => {
return this.name;
}
};

const getName = obj.getName;
const getNameArrow = obj.getNameArrow;

console.log(obj.getName());
console.log(getName());
console.log(getNameArrow());
console.log(obj.getNameArrow());

Ответ: Sarah, undefined, undefined, undefined

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