-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmultiply_array.js
40 lines (35 loc) · 1.24 KB
/
multiply_array.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
"use strict";
const is_imaginary = require("./is_imaginary.js");
const is_infinity = require("./is_infinity.js");
const is_odd = require("./is_odd.js");
const is_zero = require("./is_zero.js");
const multiply_rational = require("./multiply_rational.js");
const sign_nonzero = require("./sign_nonzero.js");
/**
* @name multiply_array
* @private
* @description Multiply an array of numbers together
* @param {Array.<String>} nums - array of numerical strings
* @returns {String} product as a numerical string
*/
function multiply_array(nums, { max_decimal_digits, infinity_times_zero = "NaN" } = {}) {
const has_inf = nums.some(n => is_infinity(n));
const has_zero = nums.some(n => is_zero(n));
if (has_inf && has_zero) {
return infinity_times_zero;
} else if (has_inf) {
const ct = nums.filter(n => sign_nonzero(n) === "-").length;
return ct % 2 === 0 ? "Infinity" : "-Infinity";
} else if (has_zero) {
return "0";
}
const imaginary = is_odd(nums.filter(n => is_imaginary(n)).length.toString());
let product = multiply_rational(
nums.map(n => n.replace(/i$/, "")),
{ max_decimal_digits }
);
if (imaginary) product += "i";
return product;
}
module.exports = multiply_array;
module.exports.default = multiply_array;