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
function* rangeGenerator(start, end, step = 1) {
let current = start;
while (current <= end) {
yield current;
current += step;
}
}
const numbers = rangeGenerator(1, 10, 2);
numbers.next();
numbers.next();
const values = [...numbers];
console.log(values);
Ответ:
JavaScript test | #JavaScript
Please open Telegram to view this post
VIEW IN TELEGRAM
function recursiveReverseString(str) {
return str === "" ? str : recursiveReverseString(str.substr(1)) + str;
}
const result = recursiveReverseString("hello");
console.log(result);
Ответ:
JavaScript test | #JavaScript
Please open Telegram to view this post
VIEW IN TELEGRAM
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);
Ответ:
Please open Telegram to view this post
VIEW IN TELEGRAM
var i = 5;
const arr = new Array(5);
arr.forEach(() => i++);
console.log(i);
Ответ:
Please open Telegram to view this post
VIEW IN TELEGRAM
function recursiveReverseString(str) {
return str === "" ? str : recursiveReverseString(str.substr(1)) + str;
}
const result = recursiveReverseString("hello");
console.log(result);
Ответ:
JavaScript test | #JavaScript
Please open Telegram to view this post
VIEW IN TELEGRAM
function createCounter() {
let count = 0;
return function(increment = 1) {
count += increment;
return count;
};
}
const counter1 = createCounter();
const counter2 = createCounter();
console.log(counter1());
console.log(counter1(5));
console.log(counter2(3));
console.log(counter1());
console.log(counter2());
Ответ:
JavaScript test | #JavaScript
Please open Telegram to view this post
VIEW IN TELEGRAM
const arr = [1, 2, 3, 4, 5];
const obj = { a: 1, b: 2, c: 3 };
const result1 = Object.keys(obj).length;
const result2 = arr.length;
delete obj.b;
obj.d = 4;
const result3 = Object.keys(obj).length;
const result4 = arr.push(6);
arr.length = 3;
const result5 = arr.length;
const result6 = Object.keys(obj).join('');
console.log(result1, result2, result3, result4, result5, result6);
Ответ:
JavaScript test | #JavaScript
Please open Telegram to view this post
VIEW IN TELEGRAM
function* counter() {
let i = 1;
while (true) {
const reset = yield i++;
if (reset) {
i = 1;
}
}
}
const gen = counter();
console.log(gen.next().value);
console.log(gen.next().value);
console.log(gen.next(true).value);
console.log(gen.next().value);
Ответ:
JavaScript test | #JavaScript
Please open Telegram to view this post
VIEW IN TELEGRAM
const promise1 = Promise.resolve('first');
const promise2 = new Promise(resolve => {
resolve('second');
});
const promise3 = Promise.resolve().then(() => 'third');
async function test() {
console.log('start');
const result1 = await promise1;
console.log(result1);
const result2 = await promise2;
console.log(result2);
const result3 = await promise3;
console.log(result3);
console.log('end');
}
test();
Ответ:
JavaScript test | #JavaScript
Please open Telegram to view this post
VIEW IN TELEGRAM
function mystery(arr, depth = 0) {
if (arr.length <= 1) return arr;
const mid = Math.floor(arr.length / 2);
const left = mystery(arr.slice(0, mid), depth + 1);
const right = mystery(arr.slice(mid), depth + 1);
const result = [];
let i = 0, j = 0;
while (i < left.length && j < right.length) {
result.push(left[i] <= right[j] ? left[i++] : right[j++]);
}
return result.concat(left.slice(i)).concat(right.slice(j));
}
const arr = [3, 1, 4, 1, 5];
console.log(mystery(arr));
Ответ:
JavaScript test | #JavaScript
Please open Telegram to view this post
VIEW IN TELEGRAM
const handler = {
get(target, prop) {
if (prop in target) {
return target[prop] * 2;
} else {
return `Property ${prop} not found`;
}
},
set(target, prop, value) {
if (typeof value === 'number') {
target[prop] = Math.round(value);
return true;
}
return false;
}
};
const obj = { a: 5, b: 10 };
const proxy = new Proxy(obj, handler);
proxy.c = 7.8;
proxy.d = "hello";
console.log(obj.c, proxy.a, proxy.x);
Ответ:
JavaScript test | #JavaScript
Please open Telegram to view this post
VIEW IN TELEGRAM