❗️Что будет на выходе?
Ответ:0 2 1 2
JavaScript test | #JavaScript & Max
function* counter() {
let i = 0;
while (true) {
const direction = yield i;
if (direction === 'up') i += 2;
else if (direction === 'down') i -= 1;
else i += 1;
}
}
const count = counter();
console.log(count.next().value);
console.log(count.next('up').value);
console.log(count.next('down').value);
console.log(count.next().value);Ответ:
JavaScript test | #JavaScript & Max
❗️Что будет на выходе:
Ответ:64
JavaScript test | #JavaScript & Max
const createMathOps = (base) => {
return {
add: (x) => base + x,
multiply: (x) => base * x
};
};
const createAdvancedMathOps = (base) => {
const basicOps = createMathOps(base);
return {
...basicOps,
square: () => basicOps.multiply(base),
addThenSquare: (x) => {
const added = basicOps.add(x);
return added * added;
}
};
};
const calculator = createAdvancedMathOps(5);
console.log(calculator.addThenSquare(3));
Ответ:
JavaScript test | #JavaScript & Max
❗️Что будет на выходе:
Ответ:120 204 52.25
JavaScript test | #JavaScript & Max
const partial = (fn, ...presetArgs) => {
return function partiallyApplied(...laterArgs) {
return fn(...presetArgs, ...laterArgs);
};
};
const calculateTax = (taxRate, discount, price) => {
const discounted = price - (price * discount) / 100;
return parseFloat((discounted + (discounted * taxRate) / 100).toFixed(2));
};
const withVAT = partial(calculateTax, 20);
const withVATandDiscount = partial(withVAT, 15);
console.log(withVAT(0, 100));
console.log(withVATandDiscount(200));
console.log(partial(calculateTax, 10, 5)(50));
Ответ:
JavaScript test | #JavaScript & Max
❗️Что будет на выходе:
Ответ:{ timeout: 0, retries: 3, logging: false, debug: true }
JavaScript test | #JavaScript & Max
function processConfig(config) {
const settings = {
timeout: config.timeout ?? 1000,
retries: config.retries ?? 3,
logging: config.logging ?? false,
debug: config.debug || true
};
return settings;
}
const userConfig = {
timeout: 0,
retries: null,
logging: false,
debug: false
};
console.log(processConfig(userConfig));
Ответ:
JavaScript test | #JavaScript & Max
❗️Что будет на выходе:
Ответ:['Alice', undefined, true, undefined]
JavaScript test | #JavaScript & Max
const user = {
profile: {
name: 'Alice',
social: null,
getDetails() {
return { verified: true };
}
}
};
const result = [
user?.profile?.name,
user?.profile?.social?.handle,
user.profile.getDetails?.()?.verified,
user?.nonExistent?.property
];
console.log(result);
Ответ:
JavaScript test | #JavaScript & Max
❗️Что будет на выходе:
Ответ:Hello, I'm Alice Hello, I'm undefined Goodbye from undefined
JavaScript test | #JavaScript & Max
const person = {
name: 'Alice',
greet() {
return `Hello, I'm ${this.name}`;
},
farewell: () => `Goodbye from ${this.name}`
};
const greetFn = person.greet;
const farewellFn = person.farewell;
console.log(person.greet());
console.log(greetFn());
console.log(farewellFn());
Ответ:
JavaScript test | #JavaScript & Max
❗️Что будет на выходе:
Ответ:A G C E B F D
JavaScript test | #JavaScript & Max
function task1() {
console.log('A');
setTimeout(() => console.log('B'), 0);
Promise.resolve().then(() => console.log('C'));
Promise.resolve().then(() => setTimeout(() => console.log('D'), 0));
Promise.resolve().then(() => console.log('E'));
setTimeout(() => console.log('F'), 0);
console.log('G');
}
task1();
Ответ:
JavaScript test | #JavaScript & Max
❗️Что будет на выходе:
Ответ:{ timeout: 0, retries: 3, logging: false, debug: true }
JavaScript test | #JavaScript & Max
function processConfig(config) {
const settings = {
timeout: config.timeout ?? 1000,
retries: config.retries ?? 3,
logging: config.logging ?? false,
debug: config.debug || true
};
return settings;
}
const userConfig = {
timeout: 0,
retries: null,
logging: false,
debug: false
};
console.log(processConfig(userConfig));
Ответ:
JavaScript test | #JavaScript & Max
❗️Что будет на выходе:
Ответ:
Invalid status
JavaScript test | #JavaScript & Max
function processData(data) {
try {
if (!data) {
throw new TypeError('Data is required');
}
if (data.status === 'error') {
throw new Error('Invalid status');
}
return data.value.toUpperCase();
} catch (err) {
if (err instanceof TypeError) {
return 'Type Error';
}
return err.message;
}
}
console.log(processData({ status: 'error', value: 'test' }));Ответ:
❗️Что будет на выходе:
Ответ:{ id: 42, name: 'Item', count: 3 }
JavaScript test | #JavaScript & Max
function analyze(...items) {
const [first, ...rest] = items;
const { length } = [...rest, 'bonus'];
const result = {
...first,
count: length,
};
console.log(result);
}
analyze({ id: 42, name: 'Item' }, 'a', 'b');Ответ:
JavaScript test | #JavaScript & Max