DEV Community

Discussion on: Daily Challenge #160 - Expression Matters

Collapse
 
elishaking profile image
King Elisha

JavaScript Solution

const expressionMatters = (a, b, c) => {

  [a, b, c].forEach((n, i) => {
    if (n < 1)
      throw new Error(`Operand at index: ${i} with value: ${n} 
is less than 1. All operands must be greater than 0 and less than 11`);
    else if (n > 10)
      throw new Error(`Operand at index: ${i} with value: ${n} 
is greater than 10. All operands must be greater than zero and less than 11`);
  });

  const results = [];
  results[0] = a * (b + c);
  results[1] = a * b * c;
  results[2] = a * b + c;
  results[3] = a + b * c;
  results[4] = (a + b) * c;
  results[5] = a + b + c;
  return Math.max(...results);
}

console.log(expressionMatters(1, 2, 3)); // 9
console.log(expressionMatters(1, 1, 1)); // 3
console.log(expressionMatters(9, 1, 1)); // 18