All files support-lib.js

60% Statements 27/45
28.57% Branches 8/28
100% Functions 6/6
69.23% Lines 18/26

Press n or j to go to the next uncovered block, b, p or k for the previous block.

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 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 8231x                     31x 5x 5x 5x 5x 5x 5x 5x 13x   5x                           31x 1x                             31x 1x                         79x             48x   31x              
const Complex = require('./complex.js')
 
/**
 * Calculates the factorial of a given number.
 *
 * @param {number} num - The number to calculate the factorial of.
 * @returns {Complex} The factorial of the given number.
 * @throws {Error} If the number has a non-zero imaginary part.
 * @throws {Error} If the number is not an integer.
 * @throws {Error} If the number is negative.
 */
const factorial = function(num) {
  Iif (num?.im && num?.im !== 0) throw new Error(`Imaginary part must be zero. Instead is ${num.im}`);
  let n = Number(num?.re) || Number(num); 
  Iif (!Number.isInteger(n)) throw new Error(`Not an Integer number ${n}`);
  Iif ( n < 0) throw new Error(`Factorial of negative number ${n}`);
  let result = Complex(1);
  Iif (n === 0) return result;
  for (let i = 1; i <= n; i++) {
    result = result.mul(i);
  }
  return Complex({re: result.re, im: num.im});
};
 
/**
 * Returns the maximum of two complex numbers.
 *
 * @param {Object} a - The first complex number.
 * @param {number} a.re - The real part of the first complex number.
 * @param {number} a.im - The imaginary part of the first complex number.
 * @param {Object} b - The second complex number.
 * @param {number} b.re - The real part of the second complex number.
 * @param {number} b.im - The imaginary part of the second complex number.
 * @returns {Object} - The maximum complex number.
 */
const max = function(a, b) {
  Eif (a.re > b.re) return a;
  if (a.re < b.re) return b;
  // If we reached here is a.re === b.re
  if (a.im > b.im) return a;
  if (a.im < b.im) return b;
  return a;
}
 
/**
 * Returns the minimum of two complex numbers.
 *
 * @param {Complex} a - The first complex number.
 * @param {Complex} b - The second complex number.
 * @returns {Complex} The minimum of the two complex numbers.
 */
const min = function(a, b) {
  Eif (a.re < b.re) return a;
  if (a.re > b.re) return b;
  // If we reached here is a.re === b.re
  if (a.im < b.im) return a;
  if (a.im > b.im) return b;
  return a;
}
 
/**
 * Prints the given values to the console.
 * @param {...*} x - The values to be printed.
 * @returns {Array} - An array containing the printed values.
 */
const print = (...x) => { console.log(...x); return x; } 
 
/**
 * Writes the provided arguments to the console.
 * @param {...*} x - The values to be written to the console.
 * @returns {Array} - An array containing the string representations of the provided values.
 */
const write = (...x) => { let r = x.map(s => s.toString()); console.log(...r); return r; } 
 
module.exports = {
  Complex,
  print,
  write,
  factorial,
  max,
  min
};