This commit is contained in:
parent
c61615f4bf
commit
7997ad399e
|
@ -1,5 +1,22 @@
|
|||
const caesar = function() {
|
||||
const caesar = function (str, num) {
|
||||
num = num%25;
|
||||
return str.split('').map(x => {
|
||||
if(x == ','
|
||||
|| x == '!'
|
||||
|| x == ' '){return x}
|
||||
let asciNum = wrap(Number(x.charCodeAt()) + num);
|
||||
return String.fromCharCode(asciNum)
|
||||
}).join('');
|
||||
}
|
||||
|
||||
function wrap(num){
|
||||
if (num < 97 && num > 90){
|
||||
return num = num%90 + 64;
|
||||
}else if(num > 122){
|
||||
return num = num % 122 + 96;
|
||||
} else {
|
||||
return num;
|
||||
}
|
||||
}
|
||||
|
||||
module.exports = caesar
|
||||
|
|
|
@ -4,19 +4,19 @@ describe('caesar', function() {
|
|||
it('works with single letters', function() {
|
||||
expect(caesar('A', 1)).toEqual('B');
|
||||
});
|
||||
xit('works with words', function() {
|
||||
it('works with words', function() {
|
||||
expect(caesar('Aaa', 1)).toEqual('Bbb');
|
||||
});
|
||||
xit('works with phrases', function() {
|
||||
it('works with phrases', function() {
|
||||
expect(caesar('Hello, World!', 5)).toEqual('Mjqqt, Btwqi!');
|
||||
});
|
||||
xit('works with negative shift', function() {
|
||||
expect(caesar('Mjqqt, Btwqi!', -5)).toEqual('Hello, World!');
|
||||
});
|
||||
xit('wraps', function() {
|
||||
it('wraps', function() {
|
||||
expect(caesar('Z', 1)).toEqual('A');
|
||||
});
|
||||
xit('works with large shift factors', function() {
|
||||
it('works with large shift factors', function() {
|
||||
expect(caesar('Hello, World!', 75)).toEqual('Ebiil, Tloia!');
|
||||
});
|
||||
xit('works with large negative shift factors', function() {
|
||||
|
|
|
@ -1,25 +1,27 @@
|
|||
function add () {
|
||||
|
||||
function add (a, b) {
|
||||
return a+b;
|
||||
}
|
||||
|
||||
function subtract () {
|
||||
|
||||
function subtract(a, b) {
|
||||
return a - b;
|
||||
}
|
||||
|
||||
function sum () {
|
||||
|
||||
function sum(arr) {
|
||||
if (arr.length <1){return 0}
|
||||
if (arr.length == 1) { return arr[0] }
|
||||
return arr.reduce((a,b) => a+b);
|
||||
}
|
||||
|
||||
function multiply () {
|
||||
|
||||
function multiply(arr) {
|
||||
return arr.reduce((a, b) => a * b);
|
||||
}
|
||||
|
||||
function power() {
|
||||
|
||||
function power(a, b) {
|
||||
return Math.pow(a, b);
|
||||
}
|
||||
|
||||
function factorial() {
|
||||
|
||||
function factorial(a) {
|
||||
return a < 2 ? 1 : a * factorial(a - 1)
|
||||
}
|
||||
|
||||
module.exports = {
|
||||
|
|
|
@ -1,77 +1,77 @@
|
|||
const calculator = require ('./calculator.js');
|
||||
const calculator = require('./calculator.js');
|
||||
|
||||
describe('add', function() {
|
||||
it('adds 0 and 0', function() {
|
||||
expect(calculator.add(0,0)).toEqual(0);
|
||||
describe('add', function () {
|
||||
it('adds 0 and 0', function () {
|
||||
expect(calculator.add(0, 0)).toEqual(0);
|
||||
});
|
||||
|
||||
xit('adds 2 and 2', function() {
|
||||
expect(calculator.add(2,2)).toEqual(4);
|
||||
it('adds 2 and 2', function () {
|
||||
expect(calculator.add(2, 2)).toEqual(4);
|
||||
});
|
||||
|
||||
xit('adds positive numbers', function() {
|
||||
expect(calculator.add(2,6)).toEqual(8);
|
||||
it('adds positive numbers', function () {
|
||||
expect(calculator.add(2, 6)).toEqual(8);
|
||||
});
|
||||
});
|
||||
|
||||
describe('subtract', function() {
|
||||
xit('subtracts numbers', function() {
|
||||
expect(calculator.subtract(10,4)).toEqual(6);
|
||||
describe('subtract', function () {
|
||||
it('subtracts numbers', function () {
|
||||
expect(calculator.subtract(10, 4)).toEqual(6);
|
||||
});
|
||||
});
|
||||
|
||||
describe('sum', function() {
|
||||
xit('computes the sum of an empty array', function() {
|
||||
describe('sum', function () {
|
||||
it('computes the sum of an empty array', function () {
|
||||
expect(calculator.sum([])).toEqual(0);
|
||||
});
|
||||
|
||||
xit('computes the sum of an array of one number', function() {
|
||||
it('computes the sum of an array of one number', function () {
|
||||
expect(calculator.sum([7])).toEqual(7);
|
||||
});
|
||||
|
||||
xit('computes the sum of an array of two numbers', function() {
|
||||
expect(calculator.sum([7,11])).toEqual(18);
|
||||
it('computes the sum of an array of two numbers', function () {
|
||||
expect(calculator.sum([7, 11])).toEqual(18);
|
||||
});
|
||||
|
||||
xit('computes the sum of an array of many numbers', function() {
|
||||
expect(calculator.sum([1,3,5,7,9])).toEqual(25);
|
||||
it('computes the sum of an array of many numbers', function () {
|
||||
expect(calculator.sum([1, 3, 5, 7, 9])).toEqual(25);
|
||||
});
|
||||
});
|
||||
|
||||
describe('multiply', function() {
|
||||
xit('multiplies two numbers', function() {
|
||||
expect(calculator.multiply([2,4])).toEqual(8);
|
||||
describe('multiply', function () {
|
||||
it('multiplies two numbers', function () {
|
||||
expect(calculator.multiply([2, 4])).toEqual(8);
|
||||
});
|
||||
|
||||
xit('multiplies several numbers', function() {
|
||||
expect(calculator.multiply([2,4,6,8,10,12,14])).toEqual(645120);
|
||||
it('multiplies several numbers', function () {
|
||||
expect(calculator.multiply([2, 4, 6, 8, 10, 12, 14])).toEqual(645120);
|
||||
});
|
||||
});
|
||||
|
||||
describe('power', function() {
|
||||
xit('raises one number to the power of another number', function() {
|
||||
expect(calculator.power(4,3)).toEqual(64); // 4 to third power is 64
|
||||
describe('power', function () {
|
||||
it('raises one number to the power of another number', function () {
|
||||
expect(calculator.power(4, 3)).toEqual(64); // 4 to third power is 64
|
||||
});
|
||||
});
|
||||
|
||||
describe('factorial', function() {
|
||||
xit('computes the factorial of 0', function() {
|
||||
describe('factorial', function () {
|
||||
it('computes the factorial of 0', function () {
|
||||
expect(calculator.factorial(0)).toEqual(1); // 0! = 1
|
||||
});
|
||||
|
||||
xit('computes the factorial of 1', function() {
|
||||
it('computes the factorial of 1', function () {
|
||||
expect(calculator.factorial(1)).toEqual(1);
|
||||
});
|
||||
|
||||
xit('computes the factorial of 2', function() {
|
||||
it('computes the factorial of 2', function () {
|
||||
expect(calculator.factorial(2)).toEqual(2);
|
||||
});
|
||||
|
||||
xit('computes the factorial of 5', function() {
|
||||
it('computes the factorial of 5', function () {
|
||||
expect(calculator.factorial(5)).toEqual(120);
|
||||
});
|
||||
|
||||
xit('computes the factorial of 10', function() {
|
||||
it('computes the factorial of 10', function () {
|
||||
expect(calculator.factorial(10)).toEqual(3628800);
|
||||
});
|
||||
});
|
||||
|
|
|
@ -1,5 +1,12 @@
|
|||
const fibonacci = function() {
|
||||
|
||||
const fibonacci = function (num) {
|
||||
if (num < 0) { return "OOPS"}
|
||||
let sum = [];
|
||||
sum[0] = 0;
|
||||
sum[1] = 1;
|
||||
for (let i = 2; i <= num; i++) {
|
||||
sum[i] = sum[i - 1] + sum[i - 2];
|
||||
}
|
||||
return sum[num];
|
||||
}
|
||||
|
||||
module.exports = fibonacci
|
||||
|
|
|
@ -4,28 +4,28 @@ describe('fibonacci', function() {
|
|||
it('works', function() {
|
||||
expect(fibonacci(4)).toEqual(3);
|
||||
});
|
||||
xit('works', function() {
|
||||
it('works', function() {
|
||||
expect(fibonacci(6)).toEqual(8);
|
||||
});
|
||||
xit('works', function() {
|
||||
it('works', function() {
|
||||
expect(fibonacci(10)).toEqual(55);
|
||||
});
|
||||
xit('works', function() {
|
||||
it('works', function() {
|
||||
expect(fibonacci(15)).toEqual(610);
|
||||
});
|
||||
xit('works', function() {
|
||||
it('works', function() {
|
||||
expect(fibonacci(25)).toEqual(75025);
|
||||
});
|
||||
xit('doesn\'t accept negatives', function() {
|
||||
it('doesn\'t accept negatives', function() {
|
||||
expect(fibonacci(-25)).toEqual("OOPS");
|
||||
});
|
||||
xit('DOES accept strings', function() {
|
||||
it('DOES accept strings', function() {
|
||||
expect(fibonacci("1")).toEqual(1);
|
||||
});
|
||||
xit('DOES accept strings', function() {
|
||||
it('DOES accept strings', function() {
|
||||
expect(fibonacci("2")).toEqual(1);
|
||||
});
|
||||
xit('DOES accept strings', function() {
|
||||
it('DOES accept strings', function() {
|
||||
expect(fibonacci("8")).toEqual(21);
|
||||
});
|
||||
});
|
||||
|
|
|
@ -1,5 +1,20 @@
|
|||
let findTheOldest = function() {
|
||||
|
||||
let findTheOldest = function (arr) {
|
||||
let biggest = -Infinity;
|
||||
let age;
|
||||
let oldest = {name: ''};
|
||||
arr.map(x => {
|
||||
if (x.yearOfDeath === undefined) {
|
||||
age = 2020 - x.yearOfBirth
|
||||
} else {
|
||||
age = x.yearOfDeath - x.yearOfBirth
|
||||
}
|
||||
if (age > biggest) {
|
||||
biggest = age;
|
||||
oldest.name = x.name;
|
||||
return biggest;
|
||||
}
|
||||
});
|
||||
return oldest;
|
||||
}
|
||||
|
||||
module.exports = findTheOldest
|
||||
|
|
|
@ -21,7 +21,7 @@ describe('findTheOldest', function() {
|
|||
]
|
||||
expect(findTheOldest(people).name).toEqual('Ray');
|
||||
});
|
||||
xit('finds the oldest person if someone is still living', function() {
|
||||
it('finds the oldest person if someone is still living', function() {
|
||||
const people = [
|
||||
{
|
||||
name: 'Carly',
|
||||
|
@ -40,7 +40,7 @@ describe('findTheOldest', function() {
|
|||
]
|
||||
expect(findTheOldest(people).name).toEqual('Ray');
|
||||
});
|
||||
xit('finds the oldest person if the OLDEST is still living', function() {
|
||||
it('finds the oldest person if the OLDEST is still living', function() {
|
||||
const people = [
|
||||
{
|
||||
name: 'Carly',
|
||||
|
|
|
@ -1,5 +1,9 @@
|
|||
const getTheTitles = function() {
|
||||
|
||||
const getTheTitles = function(arr) {
|
||||
let newArr = [];
|
||||
arr.map(x=> {
|
||||
newArr.push(x.title)
|
||||
});
|
||||
return newArr;
|
||||
}
|
||||
|
||||
module.exports = getTheTitles;
|
||||
|
|
|
@ -1,5 +1,5 @@
|
|||
const helloWorld = function() {
|
||||
return ''
|
||||
return 'Hello, World!'
|
||||
}
|
||||
|
||||
module.exports = helloWorld
|
||||
|
|
|
@ -1,5 +1,16 @@
|
|||
const leapYears = function() {
|
||||
|
||||
const leapYears = function(year) {
|
||||
if(year%100 == 0){
|
||||
if ((year / 100) % 4 == 0) {
|
||||
return true;
|
||||
} else {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
if ( year%4 == 0){
|
||||
return true;
|
||||
}else {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
module.exports = leapYears
|
||||
|
|
|
@ -4,19 +4,19 @@ describe('leapYears', function() {
|
|||
it('works with non century years', function() {
|
||||
expect(leapYears(1996)).toEqual(true);
|
||||
});
|
||||
xit('works with non century years', function() {
|
||||
it('works with non century years', function() {
|
||||
expect(leapYears(1997)).toEqual(false);
|
||||
});
|
||||
xit('works with ridiculously futuristic non century years', function() {
|
||||
it('works with ridiculously futuristic non century years', function() {
|
||||
expect(leapYears(34992)).toEqual(true);
|
||||
});
|
||||
xit('works with century years', function() {
|
||||
it('works with century years', function() {
|
||||
expect(leapYears(1900)).toEqual(false);
|
||||
});
|
||||
xit('works with century years', function() {
|
||||
it('works with century years', function() {
|
||||
expect(leapYears(1600)).toEqual(true);
|
||||
});
|
||||
xit('works with century years', function() {
|
||||
it('works with century years', function() {
|
||||
expect(leapYears(700)).toEqual(false);
|
||||
});
|
||||
});
|
||||
|
|
|
@ -1,5 +1,10 @@
|
|||
const removeFromArray = function() {
|
||||
const removeFromArray = function (arr, ...indx) {
|
||||
indx.map(x => {
|
||||
if (arr.indexOf(x) == -1){ return x}
|
||||
arr.splice(arr.indexOf(x), 1);
|
||||
})
|
||||
|
||||
return arr;
|
||||
}
|
||||
|
||||
module.exports = removeFromArray
|
||||
|
|
|
@ -4,19 +4,19 @@ describe('removeFromArray', function() {
|
|||
it('removes a single value', function() {
|
||||
expect(removeFromArray([1, 2, 3, 4], 3)).toEqual([1, 2, 4]);
|
||||
});
|
||||
xit('removes multiple values', function() {
|
||||
it('removes multiple values', function() {
|
||||
expect(removeFromArray([1, 2, 3, 4], 3, 2)).toEqual([1, 4]);
|
||||
});
|
||||
xit('ignores non present values', function() {
|
||||
it('ignores non present values', function() {
|
||||
expect(removeFromArray([1, 2, 3, 4], 7, "tacos")).toEqual([1, 2, 3, 4]);
|
||||
});
|
||||
xit('ignores non present values, but still works', function() {
|
||||
it('ignores non present values, but still works', function() {
|
||||
expect(removeFromArray([1, 2, 3, 4], 7, 2)).toEqual([1, 3, 4]);
|
||||
});
|
||||
xit('can remove all values', function() {
|
||||
it('can remove all values', function() {
|
||||
expect(removeFromArray([1, 2, 3, 4], 1, 2, 3, 4)).toEqual([]);
|
||||
});
|
||||
xit('works with strings', function() {
|
||||
it('works with strings', function() {
|
||||
expect(removeFromArray(["hey", 2, 3, "ho"], "hey", 3)).toEqual([2, "ho"]);
|
||||
});
|
||||
});
|
||||
|
|
|
@ -1,5 +1,6 @@
|
|||
const repeatString = function() {
|
||||
|
||||
const repeatString = function(str, num) {
|
||||
if (num < 0) { return 'ERROR'};
|
||||
return str.repeat(num);
|
||||
}
|
||||
|
||||
module.exports = repeatString
|
||||
|
|
|
@ -4,16 +4,16 @@ describe('repeatString', function() {
|
|||
it('repeats the string', function() {
|
||||
expect(repeatString('hey', 3)).toEqual('heyheyhey');
|
||||
});
|
||||
xit('repeats the string many times', function() {
|
||||
it('repeats the string many times', function() {
|
||||
expect(repeatString('hey', 10)).toEqual('heyheyheyheyheyheyheyheyheyhey');
|
||||
});
|
||||
xit('repeats the string 1 times', function() {
|
||||
it('repeats the string 1 times', function() {
|
||||
expect(repeatString('hey', 1)).toEqual('hey');
|
||||
});
|
||||
xit('repeats the string 0 times', function() {
|
||||
it('repeats the string 0 times', function() {
|
||||
expect(repeatString('hey', 0)).toEqual('');
|
||||
});
|
||||
xit('returns ERROR with negative numbers', function() {
|
||||
it('returns ERROR with negative numbers', function() {
|
||||
expect(repeatString('hey', -1)).toEqual('ERROR');
|
||||
});
|
||||
});
|
||||
|
|
|
@ -1,5 +1,5 @@
|
|||
const reverseString = function() {
|
||||
|
||||
const reverseString = function(str) {
|
||||
return str.split('').reverse().join('');
|
||||
}
|
||||
|
||||
module.exports = reverseString
|
||||
|
|
|
@ -5,11 +5,11 @@ describe('reverseString', function() {
|
|||
expect(reverseString('hello')).toEqual('olleh');
|
||||
});
|
||||
|
||||
xit('reverses multiple words', function() {
|
||||
it('reverses multiple words', function() {
|
||||
expect(reverseString('hello there')).toEqual('ereht olleh')
|
||||
})
|
||||
|
||||
xit('works with numbers and punctuation', function() {
|
||||
it('works with numbers and punctuation', function() {
|
||||
expect(reverseString('123! abc!')).toEqual('!cba !321')
|
||||
})
|
||||
});
|
||||
|
|
|
@ -1,5 +1,14 @@
|
|||
const sumAll = function() {
|
||||
|
||||
const sumAll = function (first, last) {
|
||||
if (first < 0) { return 'ERROR' }
|
||||
if (typeof first !== 'number' || typeof last !== 'number') { return 'ERROR' }
|
||||
first = Number(first);
|
||||
last = Number(last);
|
||||
let arr = [first,last].sort();
|
||||
let sum = 0;
|
||||
for (arr[0]; arr[0] <= arr[1]; arr[0]++) {
|
||||
sum = Number(sum) + arr[0]
|
||||
}
|
||||
return sum;
|
||||
}
|
||||
|
||||
module.exports = sumAll
|
||||
|
|
|
@ -4,19 +4,19 @@ describe('sumAll', function() {
|
|||
it('sums numbers within the range', function() {
|
||||
expect(sumAll(1, 4)).toEqual(10);
|
||||
});
|
||||
xit('works with large numbers', function() {
|
||||
it('works with large numbers', function() {
|
||||
expect(sumAll(1, 4000)).toEqual(8002000);
|
||||
});
|
||||
xit('works with larger number first', function() {
|
||||
it('works with larger number first', function() {
|
||||
expect(sumAll(123, 1)).toEqual(7626);
|
||||
});
|
||||
xit('returns ERROR with negative numbers', function() {
|
||||
it('returns ERROR with negative numbers', function() {
|
||||
expect(sumAll(-10, 4)).toEqual('ERROR');
|
||||
});
|
||||
xit('returns ERROR with non-number parameters', function() {
|
||||
it('returns ERROR with non-number parameters', function() {
|
||||
expect(sumAll(10, "90")).toEqual('ERROR');
|
||||
});
|
||||
xit('returns ERROR with non-number parameters', function() {
|
||||
it('returns ERROR with non-number parameters', function() {
|
||||
expect(sumAll(10, [90, 1])).toEqual('ERROR');
|
||||
});
|
||||
});
|
||||
|
|
Loading…
Reference in New Issue