solutions to multiple exercises

This commit is contained in:
Kira 2018-08-22 00:31:01 +00:00
parent 29bd246dad
commit afeaa66f81
10 changed files with 58 additions and 24 deletions

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,12 @@
const removeFromArray = function() { function removeFromArray() {
const argsToArray = Array.from(arguments);
const unchangedArray = argsToArray.shift();
const itemsToRemove = argsToArray;
function removeThese(arrayItem) {
return !itemsToRemove.includes(arrayItem);
}
return newArray = unchangedArray.filter(removeThese);
} }
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,11 @@
const repeatString = function() { const repeatString = function(phrase, repetitions) {
let greeting = '';
if (repetitions < 0) greeting = 'ERROR';
if (repetitions == 0) greeting = '';
for (let i = 1; i <= repetitions; i++) {
greeting += phrase;
}
return greeting;
} }
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,6 +1,6 @@
# Exercise 02 - Reverse a String. # Exercise 02 - Reverse a String.
Pretty simple, write a function called `reverseString` that returns it's input, reversed! Pretty simple, write a function called `reverseString` that returns its input, reversed!
```javascript ```javascript
reverseString('hello there') // returns 'ereht olleh' reverseString('hello there') // returns 'ereht olleh'

View File

@ -1,5 +1,8 @@
const reverseString = function() { const reverseString = function(str) {
let splitString = str.split("");
let reverseArray = splitString.reverse();
let joinArray = reverseArray.join("");
return joinArray;
} }
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,23 @@
const sumAll = function() { const sumAll = function(a, b) {
const argsToArray = Array.from(arguments);
argsToArray.sort(function(a, b){return a-b});
let firstNum = argsToArray[0];
let secondNum = argsToArray[1];
let sum = 0;
var isNegative = (element) => element < 0
var isNotNumber = (element) => typeof element != 'number'
if (argsToArray.some(isNegative)) {
return "ERROR";
} else if (argsToArray.some(isNotNumber)) {
return "ERROR";
} else {
while (firstNum <= secondNum) {
sum += firstNum++;
}
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');
}); });
}); });