Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Solutions expressing programs #1

Open
wants to merge 12 commits into
base: master
Choose a base branch
from
15 changes: 15 additions & 0 deletions js-exercises/duplicate-letters/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
## Instructions


**input**: string

**output**: false if input contains no duplicate letters else
the total count of the letter with the most
duplicates.

For Example:
```js
"abc" => false
"aba" => 2
"ababcb" => 3
```
24 changes: 24 additions & 0 deletions js-exercises/duplicate-letters/duplicateLetters.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@

function duplicateLetters(str) {
if (typeof str === 'string') {
const stringArr = str.split('');
const verificationObj = {};
for (let i = 0; i < stringArr.length; i += 1) {
if (stringArr[i] !== ' ') {
// eslint-disable-next-line no-prototype-builtins
verificationObj[stringArr[i]] = verificationObj.hasOwnProperty(stringArr[i])
? verificationObj[stringArr[i]] + 1 : 1;
}
}
const maxValue = Object.values(verificationObj).length > 0
? Math.max(...Object.values(verificationObj)) : 0;
return maxValue <= 1 ? false : maxValue;
}
// throw 'Invalid argument';
// returning string for testing purpose else would have thrown an exception
return 'Invalid argument';
}

export {
duplicateLetters,
};
14 changes: 14 additions & 0 deletions js-exercises/duplicate-letters/duplicateLetters.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import { duplicateLetters } from './duplicateLetters';

describe('2 Largest duplicate count', () => {
test('should get the correct number of duplicates', () => {
expect(duplicateLetters('qwertyuiopasdfghjkl')).toBe(false);
expect(duplicateLetters('asdfghjklqwerftyuiop')).toBe(2);
expect(duplicateLetters('asyfghjklqyerftyuiop')).toBe(3);
expect(duplicateLetters('psyfpghjklqerftyupiop')).toBe(4);
expect(duplicateLetters('psyfpghjklqerftyupiop ')).toBe(4);
expect(duplicateLetters(1099)).toBe('Invalid argument');
expect(duplicateLetters({})).toBe('Invalid argument');
expect(duplicateLetters([])).toBe('Invalid argument');
});
});
4 changes: 4 additions & 0 deletions js-exercises/is-triangle/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
# Instructions

Return a true or false for wether a triangle can be formed using the three lines.
Whatever unit you take, the unit will be consistent for all the parameters.
14 changes: 14 additions & 0 deletions js-exercises/is-triangle/isTriangle.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
function isTriangle(...args) {
if (args.length === 3 && args.filter(d => typeof d === 'number').length === 3) {
const [a, b, c] = args;
return (a ** 2 + b ** 2 === c ** 2) || (b ** 2 + c ** 2 === a ** 2)
|| (c ** 2 + a ** 2 === b ** 2);
}
// throw 'Invalid argument';
// returning string for testing purpose else would have thrown an error
return 'Invalid arguments';
}

export {
isTriangle,
};
15 changes: 15 additions & 0 deletions js-exercises/is-triangle/isTriangle.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import { isTriangle } from './isTriangle';

describe('isTriangle', () => {
it('should return true if triangle can be formed given three lines', () => {
expect(isTriangle(3, 4, 5)).toBe(true);
});

it('should return false if triangle can not be formed given three lines', () => {
expect(isTriangle(1, 4, 2)).toBe(false);
});

it('should throw an error if wrong input is given', () => {
expect(isTriangle(1, 4, '2')).toBe('Invalid arguments');
});
});
5 changes: 5 additions & 0 deletions js-exercises/sum-all/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
# Instructions

We'll pass you an array of two numbers.
Return the sum of those two numbers and all numbers between them.
The lowest number will not always come first.
19 changes: 19 additions & 0 deletions js-exercises/sum-all/sumAll.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
function sumAll(...args) {
if (args.length === 2 && args.filter(d => typeof d === 'number').length === 2) {
const valueArray = args.sort();
let i = valueArray[0];
let result = valueArray[0];
do {
i += 1;
result += i;
} while (i < valueArray[1]);
return result;
}
// throw 'Invalid argument';
// returning string for testing purpose else would have thrown an error
return 'Invalid arguments';
}

export {
sumAll,
};
18 changes: 18 additions & 0 deletions js-exercises/sum-all/sumAll.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import { sumAll } from './sumAll';

describe('sumAll', () => {
test('should return a number', () => {
expect(typeof sumAll([1, 4])).toEqual('number');
});
test('should return correct number', () => {
expect(sumAll([1, 4])).toBe(10);
expect(sumAll([4, 1])).toBe(10);
expect(sumAll([5, 10])).toBe(45);
expect(sumAll([10, 5])).toBe(45);
expect(sumAll([-10, 10])).toBe(0);
expect(sumAll([-1, 10])).toBe(54);
expect(sumAll([10, -1])).toBe(54);
expect(sumAll([10, 'ff'])).toBe('Invalid arguments');
expect(sumAll({}, [])).toBe('Invalid arguments');
});
});
9 changes: 9 additions & 0 deletions js-exercises/sum-fibs/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
Given a positive integer num, return the sum of all odd Fibonacci
numbers that are less than or equal to num.
The first two numbers in the Fibonacci sequence are 1 and 1.
Every additional number in the sequence is the sum of the two
previous numbers. The first six numbers of the Fibonacci sequence
are 1, 1, 2, 3, 5 and 8.

For example, sumFibs(10) should return 10 because all odd Fibonacci
numbers less than 10 are 1, 1, 3, and 5
23 changes: 23 additions & 0 deletions js-exercises/sum-fibs/sumFibs.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
function fibSeries(num) {
if (num === 1) {
return [0, 1];
}
const arr = fibSeries(num - 1);
arr.push(arr[arr.length - 1] + arr[arr.length - 2]);
return arr;
}

function sumFibs(num) {
if (typeof num === 'number' && num > 0) {
const filteredFibSeries = fibSeries(num).filter(d => d % 2 !== 0 && d < num);
return filteredFibSeries.length > 0 ? filteredFibSeries.reduce((a, b) => a + b) : 0;
}
// throw 'Invalid argument';
// returning string for testing purpose else would have thrown an error
return 'Invalid arguments';
}


export {
sumFibs,
};
15 changes: 15 additions & 0 deletions js-exercises/sum-fibs/sumFibs.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import { sumFibs } from './sumFibs';

describe('sumFibs', () => {
test('should return a number', () => {
expect(typeof sumFibs(1)).toEqual('number');
});

test('should return the correct number', () => {
expect(sumFibs(1000)).toBe(1785);
expect(sumFibs(4000000)).toBe(4613732);
expect(sumFibs(4)).toBe(5);
expect(sumFibs(75024)).toBe(60696);
expect(sumFibs({})).toBe('Invalid arguments');
});
});
8 changes: 8 additions & 0 deletions js-exercises/sum-primes/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@

Sum all the prime numbers up to and including the provided number.
A prime number is defined as a number greater than one and having
only two divisors, one and itself.

For example, 2 is a prime number because it's only divisible by one and two.

The provided number may not be a prime.
34 changes: 34 additions & 0 deletions js-exercises/sum-primes/sumPrimes.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@

function isPrime(num) {
for (let i = 2; i < num; i += 1) {
if (num % i === 0) {
return false;
}
}
return true;
}


function getPrimeArray(num) {
const arr = [2];
for (let i = 3; i <= num; i += 2) {
if (isPrime(i)) {
arr.push(i);
}
}
return arr;
}

function sumPrimes(num) {
if (typeof num === 'number' && num > 1) {
return getPrimeArray(num).reduce((a, b) => a + b);
}

// throw 'Invalid argument';
// returning string for testing purpose else would have thrown an error
return 'Invalid arguments';
}

export {
sumPrimes,
};
15 changes: 15 additions & 0 deletions js-exercises/sum-primes/sumPrimes.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import { sumPrimes } from './sumPrimes';

describe('sumPrimes', () => {
test('should return a number', () => {
expect(typeof sumPrimes(10)).toEqual('number');
});
test('should return the correct output', () => {
expect(sumPrimes(10)).toBe(17);
expect(sumPrimes(977)).toBe(73156);
});
test('should throw error on wrong input', () => {
expect(sumPrimes(0)).toBe('Invalid Arguments');
expect(sumPrimes({})).toBe('Invalid arguments');
});
});