const target = { name: 'Sarah', age: 25 };
const handler = {
get(obj, prop) {
if (prop === 'toString') {
return () => `Person: ${obj.name}`;
}
return Reflect.get(obj, prop);
},
has(obj, prop) {
return prop !== 'age' && Reflect.has(obj, prop);
}
};
const proxy = new Proxy(target, handler);
console.log(proxy.name);
console.log('age' in proxy);
console.log(proxy.toString());Ответ:
JavaScript test | #JavaScript
Please open Telegram to view this post
VIEW IN TELEGRAM
const numbers = [1, 2, 3, 4, 5, 6];
const result = numbers.reduce((acc, num) => {
if (num % 2 === 0) {
acc.even += num;
} else {
acc.odd *= num;
}
return acc;
}, { even: 1, odd: 1 });
console.log(result);
Ответ:
JavaScript test | #JavaScript
Please open Telegram to view this post
VIEW IN TELEGRAM
const matrix = [
[1, 2, 3],
[4, 5, 6],
[7, 8, 9],
];
let columnSums = [];
for (let i = 0; i < matrix[0].length; i++) {
let sum = 0;
for (let j = 0; j < matrix.length; j++) {
sum += matrix[i][i];
}
columnSums.push(sum);
}
console.log(columnSums);
Ответ:
[ 3, 15, 27 ]
Please open Telegram to view this post
VIEW IN TELEGRAM
const arr = [1, 2, 3];
const str = arr.join([4]);
console.log(typeof str);
console.log(str);
Ответ:
'14243'
JavaScript test | #JavaScript
Please open Telegram to view this post
VIEW IN TELEGRAM
var arr = Array.from({ length: 5 }, (v, i) => i * 2);
console.log(arr);
Ответ:
[0, 2, 4, 6, 8]
JavaScript test | #JavaScript
Please open Telegram to view this post
VIEW IN TELEGRAM
function main() {
console.log(1);
setTimeout(() => console.log(2), 0);
Promise.resolve().then(() => {
console.log(3);
setTimeout(() => console.log(4), 0);
}).then(() => console.log(5));
Promise.resolve().then(() => console.log(6));
console.log(7);
}
main();
Ответ:
JavaScript test | #JavaScript
Please open Telegram to view this post
VIEW IN TELEGRAM
const cache = new WeakMap();
const obj1 = { id: 1 };
const obj2 = { id: 2 };
cache.set(obj1, 'data1');
cache.set(obj2, 'data2');
obj2.newProp = 'test';
console.log(cache.has(obj1), cache.has(obj2), cache.has({ id: 1 }));
Ответ:
Please open Telegram to view this post
VIEW IN TELEGRAM
const obj = { length: 3 };
console.log(Object.keys(obj).length);
Ответ:
JavaScript test | #JavaScript
Please open Telegram to view this post
VIEW IN TELEGRAM
console.log('1');
Promise.resolve().then(() => {
console.log('2');
Promise.resolve().then(() => console.log('3'));
});
Promise.resolve().then(() => {
console.log('4');
});
setTimeout(() => console.log('5'), 0);
console.log('6');
Ответ:
JavaScript test | #JavaScript
Please open Telegram to view this post
VIEW IN TELEGRAM
var str="good wood food cat bat hat";
console.log( str.match(/((g|w|f)ood)|((c|b|h)at)/g) );
Ответ:
[ 'good', 'wood', 'food', 'cat', 'bat', 'hat' ]
JavaScript test | #JavaScript
Please open Telegram to view this post
VIEW IN TELEGRAM
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;
}
Ответ:
JavaScript test | #JavaScript
Please open Telegram to view this post
VIEW IN TELEGRAM
Telegram
JavaScript test
Проверка своих знаний по языку JavaScript.
Ссылка: @Portal_v_IT
Сотрудничество: @oleginc, @tatiana_inc
Канал на бирже: telega.in/c/js_test
РКН: clck.ru/3KHeYk
Ссылка: @Portal_v_IT
Сотрудничество: @oleginc, @tatiana_inc
Канал на бирже: telega.in/c/js_test
РКН: clck.ru/3KHeYk
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);
Ответ:
JavaScript test | #JavaScript
Please open Telegram to view this post
VIEW IN TELEGRAM
function* gen() {
yield 1;
yield 2;
yield 3;
}
async function asyncFunc() {
for (let value of gen()) {
await new Promise(res => setTimeout(res, 100));
console.log(value);
}
return 'done';
}
const result = asyncFunc();
console.log(result instanceof Promise);
Ответ:
JavaScript test | #JavaScript
Please open Telegram to view this post
VIEW IN TELEGRAM
const asyncFunction = async () => {
const delay = (ms) => new Promise(resolve => setTimeout(resolve, ms));
const result = await Promise.race([delay(100), delay(500)]);
return result;
};
asyncFunction().then(value => console.log(value));
Ответ:
JavaScript test | #JavaScript
Please open Telegram to view this post
VIEW IN TELEGRAM
const string = "“abc123def456ghi789";
const matches =
string.match(/\d+/g);
console. log(matches );
Ответ:
JavaScript test | #JavaScript
Please open Telegram to view this post
VIEW IN TELEGRAM
var a = 0, b = 0, arr = [3, 6, 9, 6, 9, 3];
var data = arr.some((x, i)=>{
a = arr[i];
b = arr[i + 1];
return a + b == 15
})
console.log(data)
console.log(a, b)
Ответ:
6 9
JavaScript test | #JavaScript
Please open Telegram to view this post
VIEW IN TELEGRAM
const array = [1, 2, 3, 4, 5];
const result = array.map(function(n) {
return this ? n : 1 * 2 + 3;
}, false);
console.log([...result, ...result]);
Ответ:
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);
Ответ:
JavaScript test | #JavaScript
Please open Telegram to view this post
VIEW IN TELEGRAM
const obj = { a: 1, b: { c: 2 } };
const frozen = Object.freeze(obj);
frozen.a = 99;
frozen.b.c = 88;
frozen.d = 77;
const sealed = Object.seal({ x: 10, y: 20 });
sealed.x = 30;
sealed.z = 40;
delete sealed.y;
console.log(obj.a, obj.b.c, obj.d);
console.log(sealed.x, sealed.y, sealed.z);
Ответ:
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());
Ответ:
Please open Telegram to view this post
VIEW IN TELEGRAM
console.log(MyClass);
class MyClass {
constructor() {
this.value = 42;
}
}
Ответ:
Please open Telegram to view this post
VIEW IN TELEGRAM