odin-javascript-exercises/08_calculator/calculator.js

40 lines
715 B
JavaScript
Raw Permalink Normal View History

2023-06-19 16:55:57 -07:00
const add = function (...numbers) {
return numbers.reduce((res, cur) => res + cur, 0)
}
2017-09-20 16:04:46 -07:00
2023-06-19 16:55:57 -07:00
const subtract = function (...numbers) {
return numbers.reduce((res, cur) => res - cur)
}
2017-09-20 16:04:46 -07:00
2023-06-19 16:55:57 -07:00
const sum = function (numbers) {
return add(...numbers)
}
2017-09-20 16:04:46 -07:00
2023-06-19 16:55:57 -07:00
const multiply = function (...numbers) {
return numbers.reduce((res, cur) => res * cur)
}
2023-06-19 16:55:57 -07:00
const power = function (...numbers) {
return numbers.reduce((res, cur) => res ** cur)
}
2017-09-20 16:04:46 -07:00
2023-06-19 16:55:57 -07:00
const factorial = function (number) {
if (number < 0) {
return -1
} else if (number === 0) {
return 1
} else {
return number * factorial(number - 1)
}
}
2017-09-20 16:04:46 -07:00
// Do not edit below this line
2017-09-20 16:04:46 -07:00
module.exports = {
add,
subtract,
sum,
multiply,
power,
2023-06-19 16:55:57 -07:00
factorial,
}