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

function recursiveReverseString(str) {
return str === "" ? str : recursiveReverseString(str.substr(1)) + str;
}

const result = recursiveReverseString("hello");

console.log(result);

Ответ: 'ololloellohello'

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());

Ответ: 1 6 3 7 4

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);

Ответ: 3 5 3 6 3 acd

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);

Ответ: 1, 2, 1, 2

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();

Ответ: start first second third end

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));

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

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);

Ответ: 8 10 Property x not found

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));

Ответ: 1

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

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);

Ответ: true true false undefined

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();


Ответ: TypeError: this.greet is a not a function

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();


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

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());

Ответ: yellow red green yellow red

JavaScript test | #JavaScript
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(''));

Ответ: Error: Input cannot be empty

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')
});

Ответ: 2 4 done

JavaScript test | #JavaScript
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();


Ответ: undefined 10

JavaScript test | #JavaScript
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);

Ответ: Hello, Satah

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));

Ответ: Hello, Satah

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));

Ответ: Starting... Promise { 'Data loaded' } Data loaded Cleanup Processing complete

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