diff --git a/.eslintrc.json b/.eslintrc.json new file mode 100644 index 00000000..68be0524 --- /dev/null +++ b/.eslintrc.json @@ -0,0 +1,19 @@ +{ + "env": { + "browser": true, + "commonjs": true, + "es6": true + }, + "extends": [ + "standard" + ], + "globals": { + "Atomics": "readonly", + "SharedArrayBuffer": "readonly" + }, + "parserOptions": { + "ecmaVersion": 2018 + }, + "rules": { + } +} \ No newline at end of file diff --git a/package.json b/package.json index dc2529d3..58a13fd4 100644 --- a/package.json +++ b/package.json @@ -20,11 +20,14 @@ }, "homepage": "https://github.com/MCRcodes/javascript-basics#readme", "devDependencies": { - "eslint": "^6.5.1", + "eslint": "^8.30.0", "eslint-config-airbnb-base": "^14.0.0", "eslint-config-prettier": "^6.4.0", - "eslint-plugin-import": "^2.18.2", + "eslint-config-standard": "^17.0.0", + "eslint-plugin-import": "^2.26.0", + "eslint-plugin-n": "^15.6.0", "eslint-plugin-prettier": "^3.1.1", + "eslint-plugin-promise": "^6.1.1", "jest": "^27.0.6", "prettier": "1.18.2" } diff --git a/src/__tests__/arrays.test.js b/src/__tests__/arrays.test.js index d00a5566..5d1c5841 100644 --- a/src/__tests__/arrays.test.js +++ b/src/__tests__/arrays.test.js @@ -1,3 +1,4 @@ +/* eslint-disable no-undef */ const { getNthElement, arrayToCSVString, @@ -14,132 +15,132 @@ const { removeSpaces, sumNumbers, sortByLastLetter -} = require('../arrays'); +} = require('../arrays') describe('getNthElement', () => { - const array = ['cat', 'dog', 'elephant', 'fox']; + const array = ['cat', 'dog', 'elephant', 'fox'] - xit('returns the element at the given position', () => { - expect(getNthElement(0, array)).toEqual('cat'); - expect(getNthElement(2, array)).toEqual('elephant'); - expect(getNthElement(3, array)).toEqual('fox'); - }); + it('returns the element at the given position', () => { + expect(getNthElement(0, array)).toEqual('cat') + expect(getNthElement(2, array)).toEqual('elephant') + expect(getNthElement(3, array)).toEqual('fox') + }) - xit('if n is greater than the number of elements, it cycles back to the start', () => { - expect(getNthElement(4, array)).toEqual('cat'); - expect(getNthElement(5, array)).toEqual('dog'); - }); -}); + it('if n is greater than the number of elements, it cycles back to the start', () => { + expect(getNthElement(4, array)).toEqual('cat') + expect(getNthElement(5, array)).toEqual('dog') + }) +}) describe('arrayToCSVString', () => { - xit('returns the array elements as a comma-seperated string', () => { - expect(arrayToCSVString(['a', 'b', 'c', 'd'])).toEqual('a,b,c,d'); - expect(arrayToCSVString([1, 2, 3, 4, 5])).toEqual('1,2,3,4,5'); - }); -}); + it('returns the array elements as a comma-seperated string', () => { + expect(arrayToCSVString(['a', 'b', 'c', 'd'])).toEqual('a,b,c,d') + expect(arrayToCSVString([1, 2, 3, 4, 5])).toEqual('1,2,3,4,5') + }) +}) describe('csvStringToArray', () => { - xit('converts the csv string as an array', () => { - expect(csvStringToArray('a,b,c,d')).toEqual(['a', 'b', 'c', 'd']); - expect(csvStringToArray('1,2,3,4,5')).toEqual(['1', '2', '3', '4', '5']); - }); -}); + it('converts the csv string as an array', () => { + expect(csvStringToArray('a,b,c,d')).toEqual(['a', 'b', 'c', 'd']) + expect(csvStringToArray('1,2,3,4,5')).toEqual(['1', '2', '3', '4', '5']) + }) +}) describe('addToArray', () => { - xit('adds the item to the end of the array', () => { - const array = []; - const array2 = [1, 2, 3]; + it('adds the item to the end of the array', () => { + const array = [] + const array2 = [1, 2, 3] - expect(addToArray('a', array)).toEqual(undefined); - expect(addToArray(4, array2)).toEqual(undefined); + expect(addToArray('a', array)).toEqual(undefined) + expect(addToArray(4, array2)).toEqual(undefined) - expect(array).toEqual(['a']); - expect(array2).toEqual([1, 2, 3, 4]); - }); -}); + expect(array).toEqual(['a']) + expect(array2).toEqual([1, 2, 3, 4]) + }) +}) describe('addToArray2', () => { - xit('returns a new array with the value appended', () => { - const array = ['a', 'b', 'c']; - const array2 = [1, 2, 3]; + it('returns a new array with the value appended', () => { + const array = ['a', 'b', 'c'] + const array2 = [1, 2, 3] - expect(addToArray2('d', array)).toEqual(['a', 'b', 'c', 'd']); - expect(array).toEqual(['a', 'b', 'c']); + expect(addToArray2('d', array)).toEqual(['a', 'b', 'c', 'd']) + expect(array).toEqual(['a', 'b', 'c']) - expect(addToArray2(4, array2)).toEqual([1, 2, 3, 4]); - expect(array2).toEqual([1, 2, 3]); - }); -}); + expect(addToArray2(4, array2)).toEqual([1, 2, 3, 4]) + expect(array2).toEqual([1, 2, 3]) + }) +}) describe('removeNthElement', () => { - xit('removes the element at position n', () => { - const array = ['ant', 'bison', 'cockerel', 'duck', 'elephant']; - removeNthElement(2, array); - expect(array).toEqual(['ant', 'bison', 'duck', 'elephant']); - - const arrayTwo = ['thing 1', 'thing 2', 'thing 3', 'thing 4', 'thing 5']; - removeNthElement(0, arrayTwo); - expect(arrayTwo).toEqual(['thing 2', 'thing 3', 'thing 4', 'thing 5']); - }); -}); + it('removes the element at position n', () => { + const array = ['ant', 'bison', 'cockerel', 'duck', 'elephant'] + removeNthElement(2, array) + expect(array).toEqual(['ant', 'bison', 'duck', 'elephant']) + + const arrayTwo = ['thing 1', 'thing 2', 'thing 3', 'thing 4', 'thing 5'] + removeNthElement(0, arrayTwo) + expect(arrayTwo).toEqual(['thing 2', 'thing 3', 'thing 4', 'thing 5']) + }) +}) describe('numbersToStrings', () => { - xit('converts every number in the array to a string', () => { - expect(numbersToStrings([1, 2, 3])).toEqual(['1', '2', '3']); - expect(numbersToStrings([7, 8, 9])).toEqual(['7', '8', '9']); - }); -}); + it('converts every number in the array to a string', () => { + expect(numbersToStrings([1, 2, 3])).toEqual(['1', '2', '3']) + expect(numbersToStrings([7, 8, 9])).toEqual(['7', '8', '9']) + }) +}) describe('uppercaseWordsInArray', () => { - xit('makes every string in the array uppercase', () => { + it('makes every string in the array uppercase', () => { expect(uppercaseWordsInArray(['cat', 'mouse', 'banana'])).toEqual([ 'CAT', 'MOUSE', 'BANANA' - ]); + ]) expect(uppercaseWordsInArray(['romy', 'mo', 'baNana'])).toEqual([ 'ROMY', 'MO', 'BANANA' - ]); - }); -}); + ]) + }) +}) describe('reverseWordsInArray', () => { - xit('reverses every string in an array', () => { + it('reverses every string in an array', () => { expect(reverseWordsInArray(['cat', 'Mouse', 'banana'])).toEqual([ 'tac', 'esuoM', 'ananab' - ]); + ]) expect(reverseWordsInArray(['dog', 'cat', 'fig'])).toEqual([ 'god', 'tac', 'gif' - ]); - }); -}); + ]) + }) +}) describe('onlyEven', () => { - xit('filters the array and only returns even numbers', () => { - expect(onlyEven([1, 2, 3, 4, 5, 6, 7, 8])).toEqual([2, 4, 6, 8]); - expect(onlyEven([8, 9, 10, 11, 12, 13, 14, 15])).toEqual([8, 10, 12, 14]); - }); -}); + it('filters the array and only returns even numbers', () => { + expect(onlyEven([1, 2, 3, 4, 5, 6, 7, 8])).toEqual([2, 4, 6, 8]) + expect(onlyEven([8, 9, 10, 11, 12, 13, 14, 15])).toEqual([8, 10, 12, 14]) + }) +}) describe('removeNthElement2', () => { - xit('returns an array with the nth element removed, and does not mutate the original', () => { - const array = ['bike', 'car', 'train', 'bus']; - expect(removeNthElement2(2, array)).toEqual(['bike', 'car', 'bus']); - expect(array).toEqual(['bike', 'car', 'train', 'bus']); - const arrayTwo = ['cat', 'mouse', 'banana']; - expect(removeNthElement2(0, arrayTwo)).toEqual(['mouse', 'banana']); - expect(arrayTwo).toEqual(['cat', 'mouse', 'banana']); - }); -}); + it('returns an array with the nth element removed, and does not mutate the original', () => { + const array = ['bike', 'car', 'train', 'bus'] + expect(removeNthElement2(2, array)).toEqual(['bike', 'car', 'bus']) + expect(array).toEqual(['bike', 'car', 'train', 'bus']) + const arrayTwo = ['cat', 'mouse', 'banana'] + expect(removeNthElement2(0, arrayTwo)).toEqual(['mouse', 'banana']) + expect(arrayTwo).toEqual(['cat', 'mouse', 'banana']) + }) +}) describe('elementsStartingWithAVowel', () => { - xit('returns elements starting with a vowel', () => { + it('returns elements starting with a vowel', () => { expect( elementsStartingWithAVowel([ 'apple', @@ -169,17 +170,17 @@ describe('elementsStartingWithAVowel', () => { 'yupple', 'zupple' ]) - ).toEqual(['apple', 'epple', 'ipple', 'opple', 'upple']); + ).toEqual(['apple', 'epple', 'ipple', 'opple', 'upple']) expect( elementsStartingWithAVowel([ 'aaaa', 'bbbb', - 'eeee', + 'eeee' ]) - ).toEqual(['aaaa', 'eeee']); - }); + ).toEqual(['aaaa', 'eeee']) + }) - xit('is case insensitive', () => { + it('is case insensitive', () => { expect( elementsStartingWithAVowel([ 'Apple', @@ -209,42 +210,42 @@ describe('elementsStartingWithAVowel', () => { 'Yupple', 'Zupple' ]) - ).toEqual(['Apple', 'Epple', 'Ipple', 'Opple', 'Upple']); - expect( + ).toEqual(['Apple', 'Epple', 'Ipple', 'Opple', 'Upple']) + expect( elementsStartingWithAVowel([ 'Aaaa', 'Bbbb', - 'Eeee', + 'Eeee' ]) - ).toEqual(['Aaaa', 'Eeee']); - }); -}); + ).toEqual(['Aaaa', 'Eeee']) + }) +}) describe('removeSpaces', () => { - xit('returns the string with the space characters removed', () => { + it('returns the string with the space characters removed', () => { expect(removeSpaces('this string has spaces')).toEqual( 'thisstringhasspaces' - ); + ) expect(removeSpaces(' this one has sneaky spaces ')).toEqual( 'thisonehassneakyspaces' - ); - }); -}); + ) + }) +}) describe('sumNumbers', () => { - xit('returns the sum of the numbers in the array', () => { - expect(sumNumbers([1, 3, 5, 6, 2, 8])).toEqual(25); - expect(sumNumbers([1, 3, 5])).toEqual(9); - }); -}); + it('returns the sum of the numbers in the array', () => { + expect(sumNumbers([1, 3, 5, 6, 2, 8])).toEqual(25) + expect(sumNumbers([1, 3, 5])).toEqual(9) + }) +}) describe('sortByLastLetter', () => { - xit('sorts the string by the last character', () => { + it('sorts the string by the last character', () => { expect( sortByLastLetter(['Lannister', 'Stark', 'Greyjoy', 'Targaryen']) - ).toEqual(['Stark', 'Targaryen', 'Lannister', 'Greyjoy']); + ).toEqual(['Stark', 'Targaryen', 'Lannister', 'Greyjoy']) expect( sortByLastLetter(['Mo', 'Romy', 'Miguel', 'Martyna']) - ).toEqual(['Martyna', 'Miguel', 'Mo', 'Romy']); - }); -}); + ).toEqual(['Martyna', 'Miguel', 'Mo', 'Romy']) + }) +}) diff --git a/src/__tests__/booleans.test.js b/src/__tests__/booleans.test.js index 734201d5..c669d452 100644 --- a/src/__tests__/booleans.test.js +++ b/src/__tests__/booleans.test.js @@ -1,3 +1,4 @@ +/* eslint-disable no-undef */ const { negate, both, @@ -14,149 +15,149 @@ const { startsWith, containsVowels, isLowerCase -} = require('../booleans'); +} = require('../booleans') describe('negate', () => { - xit('returns the opposite of the passed boolean value', () => { - expect(negate(true)).toBe(false); - expect(negate(false)).toBe(true); - }); -}); + it('returns the opposite of the passed boolean value', () => { + expect(negate(true)).toBe(false) + expect(negate(false)).toBe(true) + }) +}) describe('both', () => { - xit('returns true if both of the given booleans are true', () => { - expect(both(true, true)).toBe(true); - expect(both(true, false)).toBe(false); - expect(both(false, true)).toBe(false); - expect(both(false, false)).toBe(false); - }); -}); + it('returns true if both of the given booleans are true', () => { + expect(both(true, true)).toBe(true) + expect(both(true, false)).toBe(false) + expect(both(false, true)).toBe(false) + expect(both(false, false)).toBe(false) + }) +}) describe('either', () => { - xit('returns true if at least one of the given booleans are true', () => { - expect(either(true, true)).toBe(true); - expect(either(true, false)).toBe(true); - expect(either(false, true)).toBe(true); - expect(either(false, false)).toBe(false); - }); -}); + it('returns true if at least one of the given booleans are true', () => { + expect(either(true, true)).toBe(true) + expect(either(true, false)).toBe(true) + expect(either(false, true)).toBe(true) + expect(either(false, false)).toBe(false) + }) +}) describe('none', () => { - xit('returns true if neither of the given booleans are true', () => { - expect(none(true, true)).toBe(false); - expect(none(true, false)).toBe(false); - expect(none(false, true)).toBe(false); - expect(none(false, false)).toBe(true); - }); -}); + it('returns true if neither of the given booleans are true', () => { + expect(none(true, true)).toBe(false) + expect(none(true, false)).toBe(false) + expect(none(false, true)).toBe(false) + expect(none(false, false)).toBe(true) + }) +}) describe('one', () => { - xit('returns true if exactly one of the given booleans are true', () => { - expect(one(true, true)).toBe(false); - expect(one(true, false)).toBe(true); - expect(one(false, true)).toBe(true); - expect(one(false, false)).toBe(false); - }); -}); + it('returns true if exactly one of the given booleans are true', () => { + expect(one(true, true)).toBe(false) + expect(one(true, false)).toBe(true) + expect(one(false, true)).toBe(true) + expect(one(false, false)).toBe(false) + }) +}) describe('truthiness', () => { - xit('returns the truthiness of the given value', () => { - expect(truthiness('')).toBe(false); - expect(truthiness('dbbd')).toBe(true); - expect(truthiness(0)).toBe(false); - expect(truthiness(3)).toBe(true); - expect(truthiness([])).toBe(true); - expect(truthiness({})).toBe(true); - expect(truthiness(null)).toBe(false); - expect(truthiness(undefined)).toBe(false); - expect(truthiness(NaN)).toBe(false); - }); -}); + it('returns the truthiness of the given value', () => { + expect(truthiness('')).toBe(false) + expect(truthiness('dbbd')).toBe(true) + expect(truthiness(0)).toBe(false) + expect(truthiness(3)).toBe(true) + expect(truthiness([])).toBe(true) + expect(truthiness({})).toBe(true) + expect(truthiness(null)).toBe(false) + expect(truthiness(undefined)).toBe(false) + expect(truthiness(NaN)).toBe(false) + }) +}) describe('isEqual', () => { - xit('returns whether the two values are equal', () => { - expect(isEqual(true, false)).toBe(false); - expect(isEqual(true, true)).toBe(true); - expect(isEqual('true', 'true')).toBe(true); - expect(isEqual('true', 'false')).toBe(false); - expect(isEqual(10, 0)).toBe(false); - expect(isEqual(10, 10)).toBe(true); - expect(isEqual(10, '10')).toBe(false); - }); -}); + it('returns whether the two values are equal', () => { + expect(isEqual(true, false)).toBe(false) + expect(isEqual(true, true)).toBe(true) + expect(isEqual('true', 'true')).toBe(true) + expect(isEqual('true', 'false')).toBe(false) + expect(isEqual(10, 0)).toBe(false) + expect(isEqual(10, 10)).toBe(true) + expect(isEqual(10, '10')).toBe(false) + }) +}) describe('isGreaterThan', () => { - xit('returns true if the first number is strictly greater than the second', () => { - expect(isGreaterThan(1, 2)).toBe(false); - expect(isGreaterThan(3, 2)).toBe(true); - expect(isGreaterThan(4, 4)).toBe(false); - expect(isGreaterThan(-3, 4)).toBe(false); - expect(isGreaterThan(4, -3)).toBe(true); - expect(isGreaterThan(0, 1)).toBe(false); - expect(isGreaterThan(1, 0)).toBe(true); - }); -}); + it('returns true if the first number is strictly greater than the second', () => { + expect(isGreaterThan(1, 2)).toBe(false) + expect(isGreaterThan(3, 2)).toBe(true) + expect(isGreaterThan(4, 4)).toBe(false) + expect(isGreaterThan(-3, 4)).toBe(false) + expect(isGreaterThan(4, -3)).toBe(true) + expect(isGreaterThan(0, 1)).toBe(false) + expect(isGreaterThan(1, 0)).toBe(true) + }) +}) describe('isLessThanOrEqualTo', () => { - xit('returns true if the first number is less than or equal to the second', () => { - expect(isLessThanOrEqualTo(1, 2)).toBe(true); - expect(isLessThanOrEqualTo(3, 2)).toBe(false); - expect(isLessThanOrEqualTo(4, 4)).toBe(true); - expect(isLessThanOrEqualTo(0, 4)).toBe(true); - expect(isLessThanOrEqualTo(-1, 0)).toBe(true); - }); -}); + it('returns true if the first number is less than or equal to the second', () => { + expect(isLessThanOrEqualTo(1, 2)).toBe(true) + expect(isLessThanOrEqualTo(3, 2)).toBe(false) + expect(isLessThanOrEqualTo(4, 4)).toBe(true) + expect(isLessThanOrEqualTo(0, 4)).toBe(true) + expect(isLessThanOrEqualTo(-1, 0)).toBe(true) + }) +}) describe('isOdd', () => { - xit('returns whether the number is odd', () => { - expect(isOdd(5)).toBe(true); - expect(isOdd(6)).toBe(false); - expect(isOdd(7)).toBe(true); - expect(isOdd(8)).toBe(false); - }); -}); + it('returns whether the number is odd', () => { + expect(isOdd(5)).toBe(true) + expect(isOdd(6)).toBe(false) + expect(isOdd(7)).toBe(true) + expect(isOdd(8)).toBe(false) + }) +}) describe('isEven', () => { - xit('returns whether the number is even', () => { - expect(isEven(5)).toBe(false); - expect(isEven(6)).toBe(true); - expect(isEven(7)).toBe(false); - expect(isEven(8)).toBe(true); - }); -}); + it('returns whether the number is even', () => { + expect(isEven(5)).toBe(false) + expect(isEven(6)).toBe(true) + expect(isEven(7)).toBe(false) + expect(isEven(8)).toBe(true) + }) +}) describe('isSquare', () => { - xit('returns true if the number is a square', () => { - expect(isSquare(9)).toEqual(true); - expect(isSquare(4)).toEqual(true); - expect(isSquare(5)).toEqual(false); - expect(isSquare(-4)).toEqual(false); - expect(isSquare(0)).toEqual(true); - }); -}); + it('returns true if the number is a square', () => { + expect(isSquare(9)).toEqual(true) + expect(isSquare(4)).toEqual(true) + expect(isSquare(5)).toEqual(false) + expect(isSquare(-4)).toEqual(false) + expect(isSquare(0)).toEqual(true) + }) +}) describe('startsWith', () => { - xit('returns whether the given string starts with the given character', () => { - expect(startsWith('a', 'aardvark')).toBe(true); - expect(startsWith('c', 'aardvark')).toBe(false); - expect(startsWith('b', 'baardvark')).toBe(true); - expect(startsWith('a', 'qaardvark')).toBe(false); - expect(startsWith('a', 'Aardvark')).toBe(false); - }); -}); + it('returns whether the given string starts with the given character', () => { + expect(startsWith('a', 'aardvark')).toBe(true) + expect(startsWith('c', 'aardvark')).toBe(false) + expect(startsWith('b', 'baardvark')).toBe(true) + expect(startsWith('a', 'qaardvark')).toBe(false) + expect(startsWith('a', 'Aardvark')).toBe(false) + }) +}) describe('containsVowels', () => { - xit('returns whether the given string contains vowels', () => { - expect(containsVowels('cat')).toBe(true); - expect(containsVowels('DOG')).toBe(true); - expect(containsVowels('why')).toBe(false); - }); -}); + it('returns whether the given string contains vowels', () => { + expect(containsVowels('cat')).toBe(true) + expect(containsVowels('DOG')).toBe(true) + expect(containsVowels('why')).toBe(false) + }) +}) describe('isLowerCase', () => { - xit('it returns true if the given string is lowercase', () => { - expect(isLowerCase('abc')).toBe(true); - expect(isLowerCase('abc213')).toBe(true); - expect(isLowerCase('Abc')).toBe(false); - }); -}); + it('it returns true if the given string is lowercase', () => { + expect(isLowerCase('abc')).toBe(true) + expect(isLowerCase('abc213')).toBe(true) + expect(isLowerCase('Abc')).toBe(false) + }) +}) diff --git a/src/__tests__/numbers.test.js b/src/__tests__/numbers.test.js index 253de15f..8907fc73 100644 --- a/src/__tests__/numbers.test.js +++ b/src/__tests__/numbers.test.js @@ -1,3 +1,4 @@ +/* eslint-disable no-undef */ const { add, subtract, @@ -10,102 +11,102 @@ const { absolute, quotient, remainder -} = require('../numbers'); +} = require('../numbers') describe('add', () => { - xit('adds the two numbers together', () => { - expect(add(2, 1)).toEqual(3); - expect(add(15, 76)).toEqual(91); - expect(add(12, 0)).toEqual(12); - expect(add(10, -5)).toEqual(5); - }); -}); + it('adds the two numbers together', () => { + expect(add(2, 1)).toEqual(3) + expect(add(15, 76)).toEqual(91) + expect(add(12, 0)).toEqual(12) + expect(add(10, -5)).toEqual(5) + }) +}) describe('subtract', () => { - xit('subtracts the second number from the first', () => { - expect(subtract(2, 1)).toEqual(1); - expect(subtract(1, 2)).toEqual(-1); - expect(subtract(-2, 1)).toEqual(-3); - expect(subtract(1, -2)).toEqual(3); - expect(subtract(-1, -1)).toEqual(0); - }); -}); + it('subtracts the second number from the first', () => { + expect(subtract(2, 1)).toEqual(1) + expect(subtract(1, 2)).toEqual(-1) + expect(subtract(-2, 1)).toEqual(-3) + expect(subtract(1, -2)).toEqual(3) + expect(subtract(-1, -1)).toEqual(0) + }) +}) describe('multiply', () => { - xit('multiplies the two numbers together', () => { - expect(multiply(10, 3)).toEqual(30); - expect(multiply(-11, 5)).toEqual(-55); - expect(multiply(-4, -9)).toEqual(36); - }); -}); + it('multiplies the two numbers together', () => { + expect(multiply(10, 3)).toEqual(30) + expect(multiply(-11, 5)).toEqual(-55) + expect(multiply(-4, -9)).toEqual(36) + }) +}) describe('divide', () => { - xit('divides the first number by the second number', () => { - expect(divide(20, 5)).toEqual(4); - expect(divide(5, 2)).toEqual(2.5); - expect(divide(2, 5)).toEqual(0.4); - expect(divide(10, 3)).toBeCloseTo(3.33); - }); -}); + it('divides the first number by the second number', () => { + expect(divide(20, 5)).toEqual(4) + expect(divide(5, 2)).toEqual(2.5) + expect(divide(2, 5)).toEqual(0.4) + expect(divide(10, 3)).toBeCloseTo(3.33) + }) +}) describe('power', () => { - xit('returns the first number to the power of the second', () => { - expect(power(5, 2)).toEqual(25); - expect(power(2, 3)).toEqual(8); - expect(power(10, 5)).toEqual(100000); - }); -}); + it('returns the first number to the power of the second', () => { + expect(power(5, 2)).toEqual(25) + expect(power(2, 3)).toEqual(8) + expect(power(10, 5)).toEqual(100000) + }) +}) describe('round', () => { - xit('rounds the number to the nearest integer', () => { - expect(round(2.1)).toEqual(2); - expect(round(9.7)).toEqual(10); - expect(round(5.5)).toEqual(6); - }); -}); + it('rounds the number to the nearest integer', () => { + expect(round(2.1)).toEqual(2) + expect(round(9.7)).toEqual(10) + expect(round(5.5)).toEqual(6) + }) +}) describe('roundUp', () => { - xit('rounds the number up to the nearest integer', () => { - expect(roundUp(2.1)).toEqual(3); - expect(roundUp(9.7)).toEqual(10); - expect(roundUp(5.5)).toEqual(6); - }); -}); + it('rounds the number up to the nearest integer', () => { + expect(roundUp(2.1)).toEqual(3) + expect(roundUp(9.7)).toEqual(10) + expect(roundUp(5.5)).toEqual(6) + }) +}) describe('roundDown', () => { - xit('rounds the number down to the nearest integer', () => { - expect(roundDown(2.1)).toEqual(2); - expect(roundDown(9.7)).toEqual(9); - expect(roundDown(5.5)).toEqual(5); - }); -}); + it('rounds the number down to the nearest integer', () => { + expect(roundDown(2.1)).toEqual(2) + expect(roundDown(9.7)).toEqual(9) + expect(roundDown(5.5)).toEqual(5) + }) +}) describe('absolute', () => { - xit('returns the absolute value of the number', () => { - expect(absolute(-1)).toEqual(1); - expect(absolute(1)).toEqual(1); - expect(absolute(0)).toEqual(0); - }); -}); + it('returns the absolute value of the number', () => { + expect(absolute(-1)).toEqual(1) + expect(absolute(1)).toEqual(1) + expect(absolute(0)).toEqual(0) + }) +}) describe('quotient', () => { // N.B. quotient of two numbers is the integer given by dividing // the first by the second, without the remainder // 18 divided by 7 is 2 remainder 4 (or 2.571...) // so the quotient of 18 and 7 is 2 - xit('returns the quotient from dividing the first number by the second number', () => { - expect(quotient(10, 3)).toEqual(3); - expect(quotient(18, 7)).toEqual(2); - expect(quotient(77, 10)).toEqual(7); - expect(quotient(-9, 2)).toEqual(-4); - }); -}); + it('returns the quotient from dividing the first number by the second number', () => { + expect(quotient(10, 3)).toEqual(3) + expect(quotient(18, 7)).toEqual(2) + expect(quotient(77, 10)).toEqual(7) + expect(quotient(-9, 2)).toEqual(-4) + }) +}) describe('remainder', () => { - xit('returns the remainder when dividing the first number by the second number', () => { - expect(remainder(10, 3)).toEqual(1); - expect(remainder(18, 7)).toEqual(4); - expect(remainder(77, 10)).toEqual(7); - expect(remainder(-9, 2)).toEqual(-1); - }); -}); + it('returns the remainder when dividing the first number by the second number', () => { + expect(remainder(10, 3)).toEqual(1) + expect(remainder(18, 7)).toEqual(4) + expect(remainder(77, 10)).toEqual(7) + expect(remainder(-9, 2)).toEqual(-1) + }) +}) diff --git a/src/__tests__/objects.test.js b/src/__tests__/objects.test.js index 77abc373..1755397f 100644 --- a/src/__tests__/objects.test.js +++ b/src/__tests__/objects.test.js @@ -12,7 +12,7 @@ const { } = require('../objects'); describe('createPerson', () => { - xit('creates an object with the given name and age properties', () => { + it('creates an object with the given name and age properties', () => { expect(createPerson('Fred', 79)).toEqual({ name: 'Fred', age: 79 @@ -26,7 +26,7 @@ describe('createPerson', () => { }); describe('getName', () => { - xit('returns the name property of the object', () => { + it('returns the name property of the object', () => { expect( getName({ name: 'Fred', @@ -43,7 +43,7 @@ describe('getName', () => { }); describe('getProperty', () => { - xit('returns the given property', () => { + it('returns the given property', () => { expect( getProperty('age', { name: 'Fred', @@ -70,7 +70,7 @@ describe('hasProperty', () => { age: 23 }; - xit('returns true if the object has the given property', () => { + it('returns true if the object has the given property', () => { expect(hasProperty('age', fred)).toBe(true); expect(hasProperty('name', tom)).toBe(true); expect(hasProperty('favouriteColour', fred)).toBe(false); @@ -79,7 +79,7 @@ describe('hasProperty', () => { }); describe('isOver65', () => { - xit('returns true if the person is aged over 65', () => { + it('returns true if the person is aged over 65', () => { const jim = { name: 'Jim', age: 66 @@ -102,7 +102,7 @@ describe('isOver65', () => { }); describe('getAges', () => { - xit('returns the ages of each person in the array', () => { + it('returns the ages of each person in the array', () => { const jim = { name: 'Jim', age: 66 @@ -125,7 +125,7 @@ describe('getAges', () => { }); describe('findByName', () => { - xit('returns the person with the given name', () => { + it('returns the person with the given name', () => { const jim = { name: 'Jim', age: 66 @@ -147,7 +147,7 @@ describe('findByName', () => { }); describe('findHondas', () => { - xit('returns a list of cars manufactured by Honda', () => { + it('returns a list of cars manufactured by Honda', () => { const car1 = { manufacturer: 'Honda', year: 1997, @@ -179,7 +179,7 @@ describe('findHondas', () => { }); describe('averageAge', () => { - xit('returns the average age of the people in the list', () => { + it('returns the average age of the people in the list', () => { const john = { name: 'John', age: 60 @@ -207,7 +207,7 @@ describe('averageAge', () => { }); describe('createTalkingPerson', () => { - xit('returns a person who can introduce themselves', () => { + it('returns a person who can introduce themselves', () => { const bill = createTalkingPerson('Bill', 40); const catherine = createTalkingPerson('Catherine', 21); expect(bill).toEqual({ diff --git a/src/__tests__/strings.test.js b/src/__tests__/strings.test.js index e2c3c887..b19fd739 100644 --- a/src/__tests__/strings.test.js +++ b/src/__tests__/strings.test.js @@ -8,21 +8,21 @@ const { } = require('../strings'); describe('sayHello', () => { - xit('returns "Hello world!" when passed "world"', () => { + it('returns "Hello world!" when passed "world"', () => { expect(sayHello('world')).toEqual('Hello, world!'); }); - xit('returns "Hello MCR Codes!" when passed "MCR Codes"', () => { + it('returns "Hello MCR Codes!" when passed "MCR Codes"', () => { expect(sayHello('MCR Codes')).toEqual('Hello, MCR Codes!'); }); - xit('returns "Hello fsghjdfkhgf!" when passed "fsghjdfkhgf"', () => { + it('returns "Hello fsghjdfkhgf!" when passed "fsghjdfkhgf"', () => { expect(sayHello('fsghjdfkhgf')).toEqual('Hello, fsghjdfkhgf!'); }); }); describe('uppercase', () => { - xit('returns the uppercased string', () => { + it('returns the uppercased string', () => { expect(uppercase('abc')).toEqual('ABC'); expect(uppercase('def')).toEqual('DEF'); expect(uppercase('ghi')).toEqual('GHI'); @@ -30,7 +30,7 @@ describe('uppercase', () => { }); describe('lowercase', () => { - xit('returns the lowercased string', () => { + it('returns the lowercased string', () => { expect(lowercase('ABC')).toEqual('abc'); expect(lowercase('DEF')).toEqual('def'); expect(lowercase('GHI')).toEqual('ghi'); @@ -38,7 +38,7 @@ describe('lowercase', () => { }); describe('countCharacters', () => { - xit('returns the number of characters in the string', () => { + it('returns the number of characters in the string', () => { expect(countCharacters('fsfsgsfdg')).toEqual(9); expect(countCharacters('fsfsg')).toEqual(5); expect(countCharacters('')).toEqual(0); @@ -46,7 +46,7 @@ describe('countCharacters', () => { }); describe('firstCharacter', () => { - xit('returns the first character of the string', () => { + it('returns the first character of the string', () => { expect(firstCharacter('ABC')).toEqual('A'); expect(firstCharacter('DEF')).toEqual('D'); expect(firstCharacter('GHI')).toEqual('G'); @@ -54,11 +54,11 @@ describe('firstCharacter', () => { }); describe('firstCharacters', () => { - xit('returns the first 4 characters of the string', () => { + it('returns the first 4 characters of the string', () => { expect(firstCharacters('sd32fg45', 4)).toEqual('sd32'); }); - xit('returns the first 2 characters of the string', () => { + it('returns the first 2 characters of the string', () => { expect(firstCharacters('asd', 2)).toEqual('as'); }); }); diff --git a/src/arrays.js b/src/arrays.js index 822c49b7..0cf67bc7 100644 --- a/src/arrays.js +++ b/src/arrays.js @@ -1,62 +1,80 @@ const getNthElement = (index, array) => { - // your code here -}; + if (index >= array.length) { return array[index - array.length] } + return array[index] +} const arrayToCSVString = array => { - // your code here -}; + return array.toString() +} -const csvStringToArray = string => { - // your code here -}; +function csvStringToArray (string) { + return string.split(',', 5) +} -const addToArray = (element, array) => { - // your code here -}; +const addToArray = (elementX, arrayX) => { + arrayX.push(elementX) +} -const addToArray2 = (element, array) => { - // your code here -}; +const addToArray2 = (NEWelement, ORIGarray) => { + const addToArray3 = ORIGarray.concat(NEWelement) + return addToArray3 +} const removeNthElement = (index, array) => { - // your code here -}; + return array.splice(index, 1) +} const numbersToStrings = numbers => { - // your code here -}; + return numbers.map(String) +} const uppercaseWordsInArray = strings => { - // your code here -}; + return strings.map(function (A) { return A.toUpperCase() }) +} const reverseWordsInArray = strings => { - // your code here -}; + return strings.map(function (A) { return A.split('').reverse().join('') }) +} const onlyEven = numbers => { - // your code here -}; +// const numbers2 = numbers.filter(a => a % 2 === 0); works, replaced filter with map,but this gives T or F// + return numbers.filter(a => a % 2 === 0) +} +// splice() method mutates an array and returns the removed items. Splice example with no mutation. const removeNthElement2 = (index, array) => { - // your code here -}; + const newArray = [...array] + if (index >= 0) { + newArray.splice(index, 1) + return newArray + } +} const elementsStartingWithAVowel = strings => { - // your code here -}; + const results = [] + for (let i = 0; i < strings.length; i++) { + if ((strings[i].startsWith('a')) || (strings[i].startsWith('e')) || (strings[i].startsWith('i')) || + (strings[i].startsWith('o')) || (strings[i].startsWith('u')) || (strings[i].startsWith('A')) || + (strings[i].startsWith('E')) || (strings[i].startsWith('I')) || (strings[i].startsWith('O')) || + (strings[i].startsWith('U'))) { results.push(strings[i]) } + } + return results +} const removeSpaces = string => { - // your code here -}; + return string.replace(/\s+/g, '') // Research this solution +} const sumNumbers = numbers => { - // your code here -}; + const sum = numbers.reduce((acc, number) => { + return acc + number + }) + return sum +} const sortByLastLetter = strings => { - // your code here -}; + return strings.sort((a, b) => a.charCodeAt(a.length - 1) - b.charCodeAt(b.length - 1)) +} module.exports = { getNthElement, @@ -74,4 +92,4 @@ module.exports = { removeSpaces, sumNumbers, sortByLastLetter -}; +} diff --git a/src/booleans.js b/src/booleans.js index 5ff6cb55..ef053429 100644 --- a/src/booleans.js +++ b/src/booleans.js @@ -1,61 +1,68 @@ -function negate(a) { - // your code here +function negate (a) { + return !a }; -function both(a, b) { - // your code here +function both (a, b) { + return a && b }; -function either(a, b) { - // your code here +function either (a, b) { + return a || b }; -function none(a, b) { - // your code here +function none (a, b) { + return !a && !b }; -function one(a, b) { - // your code here +function one (a, b) { + return a !== b }; -function truthiness(a) { - // your code here +function truthiness (a) { + if (a) return true + return false }; -function isEqual(a, b) { - // your code here +function isEqual (a, b) { + return a === b }; -function isGreaterThan(a, b) { - // your code here +function isGreaterThan (a, b) { + return a > b }; -function isLessThanOrEqualTo(a, b) { - // your code here +function isLessThanOrEqualTo (a, b) { + return a <= b }; -function isOdd(a) { - // your code here +function isOdd (a) { + if (a % 2 !== 0) { return true } + return false }; -function isEven(a) { - // your code here +function isEven (a) { + if (a % 2 === 0) { return true } + return false }; -function isSquare(a) { - // your code here +function isSquare (a) { + if (Number.isInteger(Math.sqrt(a))) return true + return false }; -function startsWith(char, string) { - // your code here +function startsWith (char, string) { + if (char === string.charAt(0)) return true + return false }; -function containsVowels(string) { - // your code here -}; +function containsVowels (string) { + if (string.includes('a') || string.includes('e') || string.includes('O')) return true + return false +} -function isLowerCase(string) { - // your code here +function isLowerCase (string) { + if (string.toLowerCase() === string) return true + return false }; module.exports = { @@ -74,4 +81,4 @@ module.exports = { startsWith, containsVowels, isLowerCase -}; +} diff --git a/src/numbers.js b/src/numbers.js index d3eab646..8003eee3 100644 --- a/src/numbers.js +++ b/src/numbers.js @@ -1,45 +1,45 @@ function add (a, b) { - // your code here + return a + b } function subtract (a, b) { - // your code here + return a - b } function multiply (a, b) { - // your code here + return a * b } function divide (a, b) { - // your code here + return a / b } function power (a, b) { - // your code here + return a ** b } function round (a) { - // your code here + return Math.round(a) } function roundUp (a) { - // your code here + return Math.ceil(a) } function roundDown (a) { - // your code here + return Math.floor(a) } function absolute (a) { - // your code here + return Math.abs(a) } function quotient (a, b) { - // your code here + return Math.trunc(a / b) } function remainder (a, b) { - // your code here + return (a % b) } module.exports = { diff --git a/src/objects.js b/src/objects.js index 906eef8f..919a9286 100644 --- a/src/objects.js +++ b/src/objects.js @@ -1,42 +1,43 @@ const createPerson = (name, age) => { - // your code here -}; + return { + name, + age + } +} -const getName = object => { - // your code here -}; +const getName = object => object.name -const getProperty = (property, object) => { - // your code here -}; +const getProperty = (property, object) => (object[property]) -const hasProperty = (property, object) => { - // your code here -}; +const hasProperty = (property, object) => + object.hasOwnProperty(property) -const isOver65 = person => { - // your code here -}; +const isOver65 = person => person.age > 65 -const getAges = people => { - // your code here -}; +const getAges = people => people.map(person => person.age) -const findByName = (name, people) => { - // your code here -}; +const findByName = (name, people) => people.find(element => element.name === name) -const findHondas = cars => { - // your code here -}; +const findHondas = cars => cars.filter(object => object.manufacturer === 'Honda') -const averageAge = people => { - // your code here -}; +function averageAge (people) { + const totalAge = people.reduce((prevAgeAccumulator, currentValuePerson) => { + return prevAgeAccumulator + currentValuePerson.age + }, 0) + return totalAge / people.length +} +// JavaScript Array Reduce - Programming with Mosh, www.youtube.com/watch?v=g1C40tDP0Bk -const createTalkingPerson = (name, age) => { - // your code here -}; +const createTalkingPerson = (alias, number) => { + return { + name: alias, + age: number, + introduce: strangersName => { + return `Hi ${strangersName}, my name is ${alias} and I am ${number}!` + } + } +} +// Use of Template literals (Template strings) module.exports = { createPerson, @@ -49,4 +50,4 @@ module.exports = { findHondas, averageAge, createTalkingPerson -}; +} diff --git a/src/strings.js b/src/strings.js index ce02affa..0ff6ec1b 100644 --- a/src/strings.js +++ b/src/strings.js @@ -1,25 +1,25 @@ function sayHello (string) { - // your code here + return 'Hello, ' + string + '!' }; function uppercase (string) { - // your code here + return string.toUpperCase() }; function lowercase (string) { - // your code here + return string.toLowerCase() }; function countCharacters (string) { - // your code here + return string.length }; function firstCharacter (string) { - // your code here + return string.charAt(0) }; function firstCharacters (string, n) { - // your code here + return string.substring(0, n) }; module.exports = { @@ -29,4 +29,4 @@ module.exports = { countCharacters, firstCharacter, firstCharacters -}; +}