This commit is contained in:
mesutBE 2020-03-12 22:50:27 +01:00
parent c61615f4bf
commit 7997ad399e
20 changed files with 168 additions and 97 deletions

View File

@ -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 module.exports = caesar

View File

@ -4,19 +4,19 @@ describe('caesar', function() {
it('works with single letters', function() { it('works with single letters', function() {
expect(caesar('A', 1)).toEqual('B'); expect(caesar('A', 1)).toEqual('B');
}); });
xit('works with words', function() { it('works with words', function() {
expect(caesar('Aaa', 1)).toEqual('Bbb'); expect(caesar('Aaa', 1)).toEqual('Bbb');
}); });
xit('works with phrases', function() { it('works with phrases', function() {
expect(caesar('Hello, World!', 5)).toEqual('Mjqqt, Btwqi!'); expect(caesar('Hello, World!', 5)).toEqual('Mjqqt, Btwqi!');
}); });
xit('works with negative shift', function() { xit('works with negative shift', function() {
expect(caesar('Mjqqt, Btwqi!', -5)).toEqual('Hello, World!'); expect(caesar('Mjqqt, Btwqi!', -5)).toEqual('Hello, World!');
}); });
xit('wraps', function() { it('wraps', function() {
expect(caesar('Z', 1)).toEqual('A'); 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!'); expect(caesar('Hello, World!', 75)).toEqual('Ebiil, Tloia!');
}); });
xit('works with large negative shift factors', function() { xit('works with large negative shift factors', function() {

View File

@ -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 = { module.exports = {

View File

@ -5,73 +5,73 @@ describe('add', function() {
expect(calculator.add(0, 0)).toEqual(0); expect(calculator.add(0, 0)).toEqual(0);
}); });
xit('adds 2 and 2', function() { it('adds 2 and 2', function () {
expect(calculator.add(2, 2)).toEqual(4); expect(calculator.add(2, 2)).toEqual(4);
}); });
xit('adds positive numbers', function() { it('adds positive numbers', function () {
expect(calculator.add(2, 6)).toEqual(8); expect(calculator.add(2, 6)).toEqual(8);
}); });
}); });
describe('subtract', function () { describe('subtract', function () {
xit('subtracts numbers', function() { it('subtracts numbers', function () {
expect(calculator.subtract(10, 4)).toEqual(6); expect(calculator.subtract(10, 4)).toEqual(6);
}); });
}); });
describe('sum', function () { describe('sum', function () {
xit('computes the sum of an empty array', function() { it('computes the sum of an empty array', function () {
expect(calculator.sum([])).toEqual(0); 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); expect(calculator.sum([7])).toEqual(7);
}); });
xit('computes the sum of an array of two numbers', function() { it('computes the sum of an array of two numbers', function () {
expect(calculator.sum([7, 11])).toEqual(18); expect(calculator.sum([7, 11])).toEqual(18);
}); });
xit('computes the sum of an array of many numbers', function() { it('computes the sum of an array of many numbers', function () {
expect(calculator.sum([1, 3, 5, 7, 9])).toEqual(25); expect(calculator.sum([1, 3, 5, 7, 9])).toEqual(25);
}); });
}); });
describe('multiply', function () { describe('multiply', function () {
xit('multiplies two numbers', function() { it('multiplies two numbers', function () {
expect(calculator.multiply([2, 4])).toEqual(8); expect(calculator.multiply([2, 4])).toEqual(8);
}); });
xit('multiplies several numbers', function() { it('multiplies several numbers', function () {
expect(calculator.multiply([2, 4, 6, 8, 10, 12, 14])).toEqual(645120); expect(calculator.multiply([2, 4, 6, 8, 10, 12, 14])).toEqual(645120);
}); });
}); });
describe('power', function () { describe('power', function () {
xit('raises one number to the power of another number', 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 expect(calculator.power(4, 3)).toEqual(64); // 4 to third power is 64
}); });
}); });
describe('factorial', function () { describe('factorial', function () {
xit('computes the factorial of 0', function() { it('computes the factorial of 0', function () {
expect(calculator.factorial(0)).toEqual(1); // 0! = 1 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); 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); 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); 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); expect(calculator.factorial(10)).toEqual(3628800);
}); });
}); });

View File

@ -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 module.exports = fibonacci

View File

@ -4,28 +4,28 @@ describe('fibonacci', function() {
it('works', function() { it('works', function() {
expect(fibonacci(4)).toEqual(3); expect(fibonacci(4)).toEqual(3);
}); });
xit('works', function() { it('works', function() {
expect(fibonacci(6)).toEqual(8); expect(fibonacci(6)).toEqual(8);
}); });
xit('works', function() { it('works', function() {
expect(fibonacci(10)).toEqual(55); expect(fibonacci(10)).toEqual(55);
}); });
xit('works', function() { it('works', function() {
expect(fibonacci(15)).toEqual(610); expect(fibonacci(15)).toEqual(610);
}); });
xit('works', function() { it('works', function() {
expect(fibonacci(25)).toEqual(75025); expect(fibonacci(25)).toEqual(75025);
}); });
xit('doesn\'t accept negatives', function() { it('doesn\'t accept negatives', function() {
expect(fibonacci(-25)).toEqual("OOPS"); expect(fibonacci(-25)).toEqual("OOPS");
}); });
xit('DOES accept strings', function() { it('DOES accept strings', function() {
expect(fibonacci("1")).toEqual(1); expect(fibonacci("1")).toEqual(1);
}); });
xit('DOES accept strings', function() { it('DOES accept strings', function() {
expect(fibonacci("2")).toEqual(1); expect(fibonacci("2")).toEqual(1);
}); });
xit('DOES accept strings', function() { it('DOES accept strings', function() {
expect(fibonacci("8")).toEqual(21); expect(fibonacci("8")).toEqual(21);
}); });
}); });

View File

@ -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 module.exports = findTheOldest

View File

@ -21,7 +21,7 @@ describe('findTheOldest', function() {
] ]
expect(findTheOldest(people).name).toEqual('Ray'); 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 = [ const people = [
{ {
name: 'Carly', name: 'Carly',
@ -40,7 +40,7 @@ describe('findTheOldest', function() {
] ]
expect(findTheOldest(people).name).toEqual('Ray'); 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 = [ const people = [
{ {
name: 'Carly', name: 'Carly',

View File

@ -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; module.exports = getTheTitles;

View File

@ -1,5 +1,5 @@
const helloWorld = function() { const helloWorld = function() {
return '' return 'Hello, World!'
} }
module.exports = helloWorld module.exports = helloWorld

View File

@ -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 module.exports = leapYears

View File

@ -4,19 +4,19 @@ describe('leapYears', function() {
it('works with non century years', function() { it('works with non century years', function() {
expect(leapYears(1996)).toEqual(true); expect(leapYears(1996)).toEqual(true);
}); });
xit('works with non century years', function() { it('works with non century years', function() {
expect(leapYears(1997)).toEqual(false); 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); expect(leapYears(34992)).toEqual(true);
}); });
xit('works with century years', function() { it('works with century years', function() {
expect(leapYears(1900)).toEqual(false); expect(leapYears(1900)).toEqual(false);
}); });
xit('works with century years', function() { it('works with century years', function() {
expect(leapYears(1600)).toEqual(true); expect(leapYears(1600)).toEqual(true);
}); });
xit('works with century years', function() { it('works with century years', function() {
expect(leapYears(700)).toEqual(false); expect(leapYears(700)).toEqual(false);
}); });
}); });

View File

@ -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 module.exports = removeFromArray

View File

@ -4,19 +4,19 @@ describe('removeFromArray', function() {
it('removes a single value', function() { it('removes a single value', function() {
expect(removeFromArray([1, 2, 3, 4], 3)).toEqual([1, 2, 4]); 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]); 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]); 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]); 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([]); 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"]); expect(removeFromArray(["hey", 2, 3, "ho"], "hey", 3)).toEqual([2, "ho"]);
}); });
}); });

View File

@ -1,5 +1,6 @@
const repeatString = function() { const repeatString = function(str, num) {
if (num < 0) { return 'ERROR'};
return str.repeat(num);
} }
module.exports = repeatString module.exports = repeatString

View File

@ -4,16 +4,16 @@ describe('repeatString', function() {
it('repeats the string', function() { it('repeats the string', function() {
expect(repeatString('hey', 3)).toEqual('heyheyhey'); 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'); 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'); 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(''); 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'); expect(repeatString('hey', -1)).toEqual('ERROR');
}); });
}); });

View File

@ -1,5 +1,5 @@
const reverseString = function() { const reverseString = function(str) {
return str.split('').reverse().join('');
} }
module.exports = reverseString module.exports = reverseString

View File

@ -5,11 +5,11 @@ describe('reverseString', function() {
expect(reverseString('hello')).toEqual('olleh'); expect(reverseString('hello')).toEqual('olleh');
}); });
xit('reverses multiple words', function() { it('reverses multiple words', function() {
expect(reverseString('hello there')).toEqual('ereht olleh') 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') expect(reverseString('123! abc!')).toEqual('!cba !321')
}) })
}); });

View File

@ -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 module.exports = sumAll

View File

@ -4,19 +4,19 @@ describe('sumAll', function() {
it('sums numbers within the range', function() { it('sums numbers within the range', function() {
expect(sumAll(1, 4)).toEqual(10); expect(sumAll(1, 4)).toEqual(10);
}); });
xit('works with large numbers', function() { it('works with large numbers', function() {
expect(sumAll(1, 4000)).toEqual(8002000); 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); 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'); 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'); 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'); expect(sumAll(10, [90, 1])).toEqual('ERROR');
}); });
}); });