Merge 997cb06a7f
into db998d7279
This commit is contained in:
commit
4525ef1beb
|
@ -20,7 +20,25 @@ const power = function(a, b) {
|
||||||
return Math.pow(a, b);
|
return Math.pow(a, b);
|
||||||
};
|
};
|
||||||
|
|
||||||
|
/*
|
||||||
|
* If you want to write a handmade power() function by yourself,
|
||||||
|
* Here's a simple example.
|
||||||
|
* Only work with integer as input.
|
||||||
|
*/
|
||||||
|
const intPower = function(base, exponent) {
|
||||||
|
if (exponent === 0) return 1;
|
||||||
|
if (base === 0) return (exponent > 0)? 0 : Infinity;
|
||||||
|
let result = 1;
|
||||||
|
if (exponent > 0) {
|
||||||
|
while (exponent-- > 0) result *= base;
|
||||||
|
} else {
|
||||||
|
while (exponent++ < 0) result /= base;
|
||||||
|
}
|
||||||
|
return result;
|
||||||
|
};
|
||||||
|
|
||||||
const factorial = function(n) {
|
const factorial = function(n) {
|
||||||
|
if (n < 0) return undefined;
|
||||||
if (n === 0) return 1;
|
if (n === 0) return 1;
|
||||||
let product = 1;
|
let product = 1;
|
||||||
for (let i = n; i > 0; i--) {
|
for (let i = n; i > 0; i--) {
|
||||||
|
@ -32,6 +50,7 @@ const factorial = function(n) {
|
||||||
// This is another implementation of Factorial that uses recursion
|
// This is another implementation of Factorial that uses recursion
|
||||||
// THANKS to @ThirtyThreeB!
|
// THANKS to @ThirtyThreeB!
|
||||||
const recursiveFactorial = function(n) {
|
const recursiveFactorial = function(n) {
|
||||||
|
if (n < 0) return undefined;
|
||||||
if (n === 0) {
|
if (n === 0) {
|
||||||
return 1;
|
return 1;
|
||||||
}
|
}
|
||||||
|
|
|
@ -39,22 +39,62 @@ describe('sum', () => {
|
||||||
});
|
});
|
||||||
|
|
||||||
describe('multiply', () => {
|
describe('multiply', () => {
|
||||||
|
test.skip('computes the product of an empty array', () => {
|
||||||
|
expect(calculator.multiply([])).toBe(0);
|
||||||
|
});
|
||||||
test.skip('multiplies two numbers', () => {
|
test.skip('multiplies two numbers', () => {
|
||||||
expect(calculator.multiply([2,4])).toBe(8);
|
expect(calculator.multiply([2,4])).toBe(8);
|
||||||
});
|
});
|
||||||
|
|
||||||
test.skip('multiplies several numbers', () => {
|
test.skip('multiplies several numbers', () => {
|
||||||
expect(calculator.multiply([2,4,6,8,10,12,14])).toBe(645120);
|
expect(calculator.multiply([2,4,6,8,10,12,14])).toBe(645120);
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
|
/*
|
||||||
|
* Base facts:
|
||||||
|
* => x to the power of 0 is always 1.
|
||||||
|
* => with base of 0, the exponent must NOT be negative, otherwise the result will be Infinity.
|
||||||
|
*/
|
||||||
describe('power', () => {
|
describe('power', () => {
|
||||||
test.skip('raises one number to the power of another number', () => {
|
test.skip('raises one number to the power of another number', () => {
|
||||||
expect(calculator.power(4,3)).toBe(64); // 4 to third power is 64
|
expect(calculator.power(4,3)).toBe(64); // 4 to third power is 64
|
||||||
});
|
});
|
||||||
|
test.skip('to the negative exponent', () => {
|
||||||
|
expect(calculator.power(2,-2)).toBe(0.25);
|
||||||
|
});
|
||||||
|
test.skip('with negative base', () => {
|
||||||
|
expect(calculator.power(-2,2)).toBe(4);
|
||||||
|
});
|
||||||
|
test.skip('with negative base', () => {
|
||||||
|
expect(calculator.power(-2,3)).toBe(-8);
|
||||||
|
});
|
||||||
|
test.skip('negative base to the negative exponent', () => {
|
||||||
|
expect(calculator.power(-2,-2)).toBe(0.25);
|
||||||
|
});
|
||||||
|
test.skip('negative base to the negative exponent', () => {
|
||||||
|
expect(calculator.power(-2,-3)).toBe(-0.125);
|
||||||
|
});
|
||||||
|
test.skip('to the power of 0', () => {
|
||||||
|
expect(calculator.power(2,0)).toBe(1);
|
||||||
|
});
|
||||||
|
test.skip('to the power of 0', () => {
|
||||||
|
expect(calculator.power(-2,0)).toBe(1);
|
||||||
|
});
|
||||||
|
test.skip('to the power of 0', () => {
|
||||||
|
expect(calculator.power(0,0)).toBe(1);
|
||||||
|
});
|
||||||
|
test.skip('with base of 0', () => {
|
||||||
|
expect(calculator.power(0,2)).toBe(0);
|
||||||
|
});
|
||||||
|
test.skip('with base of 0', () => {
|
||||||
|
expect(calculator.power(0,-2)).toBe(Infinity);
|
||||||
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
describe('factorial', () => {
|
describe('factorial', () => {
|
||||||
|
test.skip('factorial of negative number should be undefined', () => {
|
||||||
|
expect(calculator.factorial(-1)).toBeUndefined();
|
||||||
|
});
|
||||||
test.skip('computes the factorial of 0', () => {
|
test.skip('computes the factorial of 0', () => {
|
||||||
expect(calculator.factorial(0)).toBe(1); // 0! = 1
|
expect(calculator.factorial(0)).toBe(1); // 0! = 1
|
||||||
});
|
});
|
||||||
|
|
Loading…
Reference in New Issue