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
const wMap = new WeakMap();
function foo() {
wMap.set(this, 1);
};
const bar = () => wMap.set(this, 2);
bar();
foo();
console.log(wMap.get(this));
Ответ:
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 ws = new WeakSet();
const obj1 = { name: 'first' };
const obj2 = { name: 'second' };
const obj3 = obj1;
ws.add(obj1);
ws.add(obj2);
ws.add(obj3);
console.log(ws.has(obj1));
console.log(ws.has(obj3));
console.log(ws.has({ name: 'first' }));
console.log(ws.size);
Ответ:
JavaScript test | #JavaScript
Please open Telegram to view this post
VIEW IN TELEGRAM
const obj = {
name: 'Taylor',
greet() {
return `Hello, ${this.name}!`;
},
delayedGreet() {
setTimeout(function() {
console.log(this.greet());
}, 100);
}
};
obj.delayedGreet();
Ответ:
JavaScript test | #JavaScript
Please open Telegram to view this post
VIEW IN TELEGRAM
const obj = {
name: 'Sarah',
greet: () => {
console.log(`Hello, ${this.name}`);
},
sayHi: function() {
const inner = () => {
console.log(`Hi, ${this.name}`);
};
inner();
}
};
obj.greet();
obj.sayHi();Ответ:
JavaScript test | #JavaScript
Please open Telegram to view this post
VIEW IN TELEGRAM
class LightMachine {
constructor() {
this.states = {
green: { next: 'yellow' },
yellow: { next: 'red' },
red: { next: 'green' }
};
this.currentState = 'green';
}
transition() {
this.currentState = this.states[this.currentState].next;
return this.currentState;
}
}
const lightMachine = new LightMachine();
let result = '';
for (let i = 0; i < 5; i++) {
result += lightMachine.transition() + ' ';
}
console.log(result.trim());
Ответ:
Please open Telegram to view this post
VIEW IN TELEGRAM
function processData(input) {
try {
if (typeof input !== 'string') {
throw new TypeError('Input must be a string');
}
if (input.length === 0) {
throw new Error('Input cannot be empty');
}
return input.toUpperCase();
} catch (error) {
if (error instanceof TypeError) {
return `Type error: ${error.message}`;
}
return `Error: ${error.message}`;
}
}
console.log(processData(''));
Ответ:
JavaScript test | #JavaScript
Please open Telegram to view this post
VIEW IN TELEGRAM
let symbol1 = Symbol('description');
let symbol2 = Symbol('description');
const obj = {
[symbol1]: 'value1',
[symbol2]: 'value2'
};
console.log(obj[symbol1]);
console.log(symbol1 === symbol2);
Ответ:
'value1' false
JavaScript test | #JavaScript
Please open Telegram to view this post
VIEW IN TELEGRAM
class Observable {
constructor(subscribe) {
this.subscribe = subscribe;
}
map(fn) {
return new Observable(observer => {
return this.subscribe({
next: value => observer.next(fn(value)),
error: err => observer.error(err),
complete: () => observer.complete()
});
});
}
}
const source = new Observable(observer => {
observer.next(1);
observer.next(2);
observer.complete();
});
const doubled = source.map(x => x * 2);
doubled.subscribe({
next: value => console.log(value),
complete: () => console.log('done')
});
Ответ:
Please open Telegram to view this post
VIEW IN TELEGRAM
var a = 5;
function test() {
console.log(a);
var a = 10;
console.log(a);
}
test();
Ответ:
Please open Telegram to view this post
VIEW IN TELEGRAM
function plus(a,b){
console.log(`${a} + ${b} = ${a+b}`);
}
plus(1,2);
plus(3,4);
Ответ:
1 + 2 = 3
3 + 4 = 7
JavaScript test | #JavaScript
Please open Telegram to view this post
VIEW IN TELEGRAM
const obj = {
name: 'Sarah',
greet() {
return `Hello, ${this.name}`;
},
delayedGreet() {
const fn = function() {
return this.greet();
};
return fn.call(this);
}
};
const result = obj.delayedGreet();
console.log(result);
Ответ:
JavaScript test | #JavaScript
Please open Telegram to view this post
VIEW IN TELEGRAM
class Calculator {
static multiply(a, b) {
return a * b;
}
static add = (a, b) => {
return a + b;
}
}
class ScientificCalculator extends Calculator {
static multiply(a, b) {
return super.multiply(a, b) * 2;
}
}
console.log(Calculator.multiply(3, 4));
console.log(ScientificCalculator.add(5, 6));
console.log(ScientificCalculator.multiply(2, 3));
Ответ:
JavaScript test | #JavaScript
Please open Telegram to view this post
VIEW IN TELEGRAM
async function fetchData() {
return 'Data loaded';
}
async function processData() {
console.log('Starting...');
try {
const result = fetchData();
console.log(result);
console.log(await result);
return 'Processing complete';
} catch (error) {
return 'Error occurred';
} finally {
console.log('Cleanup');
}
}
processData().then(result => console.log(result));
Ответ:
Please open Telegram to view this post
VIEW IN TELEGRAM
var foo = {};
var bar = Object.create(foo);
foo.a = 1;
console.log(bar.a);
Ответ:
JavaScript test | #JavaScript
Please open Telegram to view this post
VIEW IN TELEGRAM