Задать вопрос
@Speakermen

Почему функция ничего не возвращает?

/*
const calculate = (x, y) => ({
  multiplication: (x, y) => x * y,
  division: (x, y) => x / y,
  subtraction: (x, y) => x - y,
  addition: (x, y) => x + y,
});

console.log(calculate().multiplication(2, 2));
console.log(calculate().division(2, 2));
console.log(calculate().subtraction(2, 2));
console.log(calculate().addition(2, 2));
*/

/*
const calculate = (type) => {
  switch (type) {
    case 'multiplication':
      return (params = { x: 0, y: 0 }) => params.x * params.y;
    case 'division':
      return (params = { x: 0, y: 0 }) => params.x / params.y;
    case 'subtraction':
      return (params = { x: 0, y: 0 }) => params.x - params.y;
    case 'addition':
      return (params = { x: 0, y: 0 }) => params.x + params.y;
  }

  throw new Error('There is no such function');
};

console.log(calculate('multiplication')({ x: 2, y: 2 }));
console.log(calculate('division')({ x: 2, y: 2 }));
console.log(calculate('subtraction')({ x: 2, y: 2 }));
console.log(calculate('addition')({ x: 2, y: 2 }));
*/

/*
const calculate = (operands = { x: 0, y: 0 }) => ({
  multiplication: () => operands.x * operands.y,
  division: () => operands.x / operands.y,
  subtraction: () => operands.x - operands.y,
  addition: () => operands.x + operands.y,
});

console.log(calculate({ x: 2, y: 2 }).multiplication());
console.log(calculate({ x: 2, y: 2 }).division());
console.log(calculate({ x: 2, y: 2 }).subtraction());
console.log(calculate({ x: 2, y: 2 }).addition());
*/

/*
const multiplication = (x, y) => x * y;
const division = (x, y) => x / y;
const subtraction = (x, y) => x - y;
const addition = (x, y) => x + y;

const calculate = (operands = { x, y }, fn) => fn(operands.x, operands.y);
console.log(calculate({ x: 2, y: 2 }, multiplication));
console.log(calculate({ x: 2, y: 2 }, division));
console.log(calculate({ x: 2, y: 2 }, subtraction));
console.log(calculate({ x: 2, y: 2 }, addition));

calculate = (fn) => fn(0, 0);

console.log(calculate((x, y) => 2 - 2));
console.log(calculate((x, y) => 2 + 2));
console.log(calculate((x, y) => 2 / 2));
console.log(calculate((x, y) => 2 * 2));
*/

const multiplication = (x, y) => x * y;
const division = (x, y) => x / y;
const subtraction = (x, y) => x - y;
const addition = (x, y) => x + y;

const arrayFunction = [multiplication, division, subtraction, addition];

const calculate = (operands = { x, y }, arr) => {
  return arr.forEach((fn) => {
    return fn(operands.x, operands.y);
  });
};

console.log(calculate({ x: 2, y: 2 }, arrayFunction));
  • Вопрос задан
  • 169 просмотров
Подписаться 1 Простой Комментировать
Решения вопроса 1
0xD34F
@0xD34F Куратор тега JavaScript
return arr.forEach((fn) => {

Понятно.

Вот дураки вроде вас: раз дурак, два дурак, три дурак, четыре, пять.
Ответ написан
Пригласить эксперта
Ваш ответ на вопрос

Войдите, чтобы написать ответ

Похожие вопросы