diff --git a/README.md b/README.md deleted file mode 100644 index e4c91a5c..00000000 --- a/README.md +++ /dev/null @@ -1,88 +0,0 @@ -# JavaScript Basics - -The repository contains test cases and empty function definitions which need to be populated to solve the different challenges set in this weeks walkthrough. 🚨*You should not update the tests* 🚨 - -## Getting started - -### Clone the repository -First up, create a fork of this repo. - -Then clone your copy of the repo: -- `git clone git@github.com:*your-github-username*/javascript-basics.git` - -Then `cd` into the repository. - -You will see a few files and directory in here. Most interestingly we have: -- `src` directory - this is where our code lives -- `test` directory - this is where our test code lives -- a `package.json` file, which contains some configuration for our project - -### Install the project dependencies -The `npm install` command (or `npm i` for short) will tell npm (node package manager) to download the `dependencies` and `devDependencies` for this project, which are defined in the `package.json` file. - -**Take a look in `package.json` - what dependencies does this project have?** - -Now run the `npm install` command. Once it has completed, you should see a new `node_modules` directory in the repository. This is where our dependencies live. It's important that we don't add this directory to git, since it can contain 100000's of files that we don't need to maintain, and can just be downloaded. - -In the `package.json` file you can see that there are only devDependencies for this project. - -### Run the test code -Jest is a JavaScript testing framework - it allows us to write automated tests that -1. describe how our code should behave -2. assert that is _does_ behave in the desired way - -Once you have installed the dependencies, run `npm test` or `npm t` for short. This command will run the `test` script defined in our `package.json`, which simply runs Jest. When we run Jest, it will execute the test code written in the `test` directory. For now, you should see a message saying that all of the tests were skipped. - -Take a look at the files in the `test` directory - you will see a few files in here, all ending with `.test.js`. These map to the files in the `src` directory, and contain the tests for these files. - -For example `test/strings.test.js` contains the tests for the functions defined in `src/strings.js`. - -Take a look at the `test/strings.test.js`. At the top, we have a `require` statement. This imports the functions from our `src/strings.js` file into the test file, so that the tests can use the functions we have written in that file. - -Beneath the `require`s are the actual tests. - -These are broken into `describe` blocks, which group the tests for each function together. Each `describe` block contains some `it` blocks, which define an individual test, along with . (At the moment, these should actually be `xit` blocks... more on that imminently). - -The individual `it` blocks make some assertions about what should happen when we call the function we are testing. - -For example: - -```js -describe('sayHello', () => { - it('returns "Hello world!" when passed "world"', () => { - expect(sayHello('world')).toEqual('Hello, world!'); - }); -}); -``` - -- `describe('sayHello', () => { ... });` tells us that all of the code inside the block is concerned with testing the `sayHello` function. -- `it('returns "Hello world!" when passed "world"', () => { ... });` tells us what the return value of the function should be when passed a certain argument. -- `expect(sayHello('world')).toEqual('Hello, world!');` this line invokes the function with the string `'world` as an argument, and make an assertion about the return value of this action - it should equal `'Hello, world!'`. - -The reason all of the tests were skipped is because we have used `xit` instead of `it`. Change the `xit` on the first test to an `it` and then run `npm test` in your terminal again. You should now see the test run, and fail - with the following error message: - -``` -Expected value to equal: - "Hello, world!" - Received: - undefined - - Difference: - Comparing two different types of values. Expected string but received undefined. -``` - -This tells us that calling the `helloWorld` function with the string `'world'` returned `undefined`, but the test was expecting it to return the string `'Hello, world!'` - -Now take a look at `src/strings.js` and in particular at the `sayHello` function: - -```js -const sayHello = (string) => { - // your code here -}; -``` - -Can you see why we got the test result that we did? - -### Update the function so that the test pass - -You challenge is to write some code in the `sayHello` function in `src/strings.js` that will make the test pass. 🚨*You should not update the tests* 🚨 diff --git a/package.json b/package.json old mode 100644 new mode 100755 diff --git a/src/__tests__/arrays.test.js b/src/__tests__/arrays.test.js index d00a5566..9b4f4b06 100644 --- a/src/__tests__/arrays.test.js +++ b/src/__tests__/arrays.test.js @@ -19,7 +19,7 @@ const { describe('getNthElement', () => { const array = ['cat', 'dog', 'elephant', 'fox']; - xit('returns the element at the given position', () => { + 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'); @@ -32,21 +32,21 @@ describe('getNthElement', () => { }); describe('arrayToCSVString', () => { - xit('returns the array elements as a comma-seperated string', () => { + 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', () => { + 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', () => { + it('adds the item to the end of the array', () => { const array = []; const array2 = [1, 2, 3]; @@ -59,7 +59,7 @@ describe('addToArray', () => { }); describe('addToArray2', () => { - xit('returns a new array with the value appended', () => { + it('returns a new array with the value appended', () => { const array = ['a', 'b', 'c']; const array2 = [1, 2, 3]; @@ -72,11 +72,11 @@ describe('addToArray2', () => { }); describe('removeNthElement', () => { - xit('removes the element at position n', () => { + 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']); @@ -84,14 +84,14 @@ describe('removeNthElement', () => { }); describe('numbersToStrings', () => { - xit('converts every number in the array to a string', () => { + 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', @@ -121,7 +121,7 @@ describe('reverseWordsInArray', () => { }); describe('onlyEven', () => { - xit('filters the array and only returns even numbers', () => { + 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]); }); @@ -139,7 +139,7 @@ describe('removeNthElement2', () => { }); describe('elementsStartingWithAVowel', () => { - xit('returns elements starting with a vowel', () => { + it('returns elements starting with a vowel', () => { expect( elementsStartingWithAVowel([ 'apple', @@ -170,16 +170,13 @@ describe('elementsStartingWithAVowel', () => { 'zupple' ]) ).toEqual(['apple', 'epple', 'ipple', 'opple', 'upple']); - expect( - elementsStartingWithAVowel([ - 'aaaa', - 'bbbb', - 'eeee', - ]) - ).toEqual(['aaaa', 'eeee']); + expect(elementsStartingWithAVowel(['aaaa', 'bbbb', 'eeee'])).toEqual([ + 'aaaa', + 'eeee' + ]); }); - xit('is case insensitive', () => { + it('is case insensitive', () => { expect( elementsStartingWithAVowel([ 'Apple', @@ -210,18 +207,15 @@ describe('elementsStartingWithAVowel', () => { 'Zupple' ]) ).toEqual(['Apple', 'Epple', 'Ipple', 'Opple', 'Upple']); - expect( - elementsStartingWithAVowel([ - 'Aaaa', - 'Bbbb', - 'Eeee', - ]) - ).toEqual(['Aaaa', 'Eeee']); + expect(elementsStartingWithAVowel(['Aaaa', 'Bbbb', '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' ); @@ -232,7 +226,7 @@ describe('removeSpaces', () => { }); describe('sumNumbers', () => { - xit('returns the sum of the numbers in the array', () => { + 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); }); @@ -243,8 +237,11 @@ describe('sortByLastLetter', () => { expect( sortByLastLetter(['Lannister', 'Stark', 'Greyjoy', 'Targaryen']) ).toEqual(['Stark', 'Targaryen', 'Lannister', 'Greyjoy']); - expect( - sortByLastLetter(['Mo', 'Romy', 'Miguel', 'Martyna']) - ).toEqual(['Martyna', 'Miguel', 'Mo', 'Romy']); + expect(sortByLastLetter(['Mo', 'Romy', 'Miguel', 'Martyna'])).toEqual([ + 'Martyna', + 'Miguel', + 'Mo', + 'Romy' + ]); }); }); diff --git a/src/__tests__/booleans.test.js b/src/__tests__/booleans.test.js index 734201d5..d6477555 100644 --- a/src/__tests__/booleans.test.js +++ b/src/__tests__/booleans.test.js @@ -17,14 +17,14 @@ const { } = require('../booleans'); describe('negate', () => { - xit('returns the opposite of the passed boolean value', () => { + 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', () => { + 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); @@ -33,7 +33,7 @@ describe('both', () => { }); describe('either', () => { - xit('returns true if at least one of the given booleans are true', () => { + 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); @@ -42,7 +42,7 @@ describe('either', () => { }); describe('none', () => { - xit('returns true if neither of the given booleans are 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); @@ -51,7 +51,7 @@ describe('none', () => { }); describe('one', () => { - xit('returns true if exactly one of the given booleans are true', () => { + 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); @@ -60,7 +60,7 @@ describe('one', () => { }); describe('truthiness', () => { - xit('returns the truthiness of the given value', () => { + it('returns the truthiness of the given value', () => { expect(truthiness('')).toBe(false); expect(truthiness('dbbd')).toBe(true); expect(truthiness(0)).toBe(false); @@ -74,7 +74,7 @@ describe('truthiness', () => { }); describe('isEqual', () => { - xit('returns whether the two values are equal', () => { + 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); @@ -86,7 +86,7 @@ describe('isEqual', () => { }); describe('isGreaterThan', () => { - xit('returns true if the first number is strictly greater than the second', () => { + 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); @@ -98,7 +98,7 @@ describe('isGreaterThan', () => { }); describe('isLessThanOrEqualTo', () => { - xit('returns true if the first number is less than or equal to the second', () => { + 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); @@ -108,7 +108,7 @@ describe('isLessThanOrEqualTo', () => { }); describe('isOdd', () => { - xit('returns whether the number is odd', () => { + it('returns whether the number is odd', () => { expect(isOdd(5)).toBe(true); expect(isOdd(6)).toBe(false); expect(isOdd(7)).toBe(true); @@ -117,7 +117,7 @@ describe('isOdd', () => { }); describe('isEven', () => { - xit('returns whether the number is even', () => { + it('returns whether the number is even', () => { expect(isEven(5)).toBe(false); expect(isEven(6)).toBe(true); expect(isEven(7)).toBe(false); @@ -126,7 +126,7 @@ describe('isEven', () => { }); describe('isSquare', () => { - xit('returns true if the number is a square', () => { + it('returns true if the number is a square', () => { expect(isSquare(9)).toEqual(true); expect(isSquare(4)).toEqual(true); expect(isSquare(5)).toEqual(false); @@ -136,7 +136,7 @@ describe('isSquare', () => { }); describe('startsWith', () => { - xit('returns whether the given string starts with the given character', () => { + 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); @@ -146,7 +146,7 @@ describe('startsWith', () => { }); describe('containsVowels', () => { - xit('returns whether the given string contains vowels', () => { + it('returns whether the given string contains vowels', () => { expect(containsVowels('cat')).toBe(true); expect(containsVowels('DOG')).toBe(true); expect(containsVowels('why')).toBe(false); @@ -154,7 +154,7 @@ describe('containsVowels', () => { }); describe('isLowerCase', () => { - xit('it returns true if the given string is lowercase', () => { + 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..d610b3b3 100644 --- a/src/__tests__/numbers.test.js +++ b/src/__tests__/numbers.test.js @@ -13,7 +13,7 @@ const { } = require('../numbers'); describe('add', () => { - xit('adds the two numbers together', () => { + it('adds the two numbers together', () => { expect(add(2, 1)).toEqual(3); expect(add(15, 76)).toEqual(91); expect(add(12, 0)).toEqual(12); @@ -22,7 +22,7 @@ describe('add', () => { }); describe('subtract', () => { - xit('subtracts the second number from the first', () => { + 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); @@ -32,7 +32,7 @@ describe('subtract', () => { }); describe('multiply', () => { - xit('multiplies the two numbers together', () => { + it('multiplies the two numbers together', () => { expect(multiply(10, 3)).toEqual(30); expect(multiply(-11, 5)).toEqual(-55); expect(multiply(-4, -9)).toEqual(36); @@ -40,7 +40,7 @@ describe('multiply', () => { }); describe('divide', () => { - xit('divides the first number by the second number', () => { + 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); @@ -49,7 +49,7 @@ describe('divide', () => { }); describe('power', () => { - xit('returns the first number to the power of the second', () => { + 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); @@ -57,7 +57,7 @@ describe('power', () => { }); describe('round', () => { - xit('rounds the number to the nearest integer', () => { + 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); @@ -65,7 +65,7 @@ describe('round', () => { }); describe('roundUp', () => { - xit('rounds the number up to the nearest integer', () => { + 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); @@ -73,7 +73,7 @@ describe('roundUp', () => { }); describe('roundDown', () => { - xit('rounds the number down to the nearest integer', () => { + 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); @@ -81,7 +81,7 @@ describe('roundDown', () => { }); describe('absolute', () => { - xit('returns the absolute value of the number', () => { + it('returns the absolute value of the number', () => { expect(absolute(-1)).toEqual(1); expect(absolute(1)).toEqual(1); expect(absolute(0)).toEqual(0); @@ -93,7 +93,7 @@ describe('quotient', () => { // 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', () => { + 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); @@ -102,7 +102,7 @@ describe('quotient', () => { }); describe('remainder', () => { - xit('returns the remainder when dividing the first number by the second number', () => { + 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); 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..9eeea833 100644 --- a/src/arrays.js +++ b/src/arrays.js @@ -1,57 +1,84 @@ const getNthElement = (index, array) => { // your code here + return array[index]; }; const arrayToCSVString = array => { // your code here + return array.toString(); }; const csvStringToArray = string => { // your code here + return string.split(','); }; const addToArray = (element, array) => { // your code here + array.push(element); }; const addToArray2 = (element, array) => { // your code here + let newArray = [].concat(array); + newArray.push(element); + return newArray; }; const removeNthElement = (index, array) => { // your code here + array.splice(index, 1); + return array; }; const numbersToStrings = numbers => { // your code here + return numbers.toString().split(','); }; const uppercaseWordsInArray = strings => { // your code here + const upper = strings.map(element => { + return element.toUpperCase(); + }); + return upper; }; const reverseWordsInArray = strings => { // your code here + // console.log(strings); + // result = strings.toString(); + // console.log(result); }; const onlyEven = numbers => { // your code here + const even = numbers.filter(number => { + return number % 2 === 0; + }); + return even; }; const removeNthElement2 = (index, array) => { // your code here + // removedItem = array.splice(index, 1); + // console.log(array); + // return array; }; const elementsStartingWithAVowel = strings => { // your code here + return strings.filter(str => /^[aeiou]/i.test(str)); }; const removeSpaces = string => { // your code here + return string.replace(/ /g, ''); }; const sumNumbers = numbers => { // your code here + return numbers.reduce((a, b) => a + b, 0); }; const sortByLastLetter = strings => { diff --git a/src/booleans.js b/src/booleans.js index 5ff6cb55..9bf0ccc5 100644 --- a/src/booleans.js +++ b/src/booleans.js @@ -1,62 +1,113 @@ function negate(a) { // your code here -}; + if (a) { + return false; + } else { + return true; + } +} function both(a, b) { // your code here -}; + if (a && b) { + return true; + } else { + return false; + } +} function either(a, b) { // your code here -}; + if (a || b) { + return true; + } else { + return false; + } +} function none(a, b) { // your code here -}; + if (a && b) { + return false; + } else if (a || b) { + return false; + } else { + return true; + } +} function one(a, b) { // your code here -}; + if (a && b) { + return false; + } else if (a || b) { + return true; + } else { + return false; + } +} function truthiness(a) { - // your code here -}; + return !!a; +} function isEqual(a, b) { // your code here -}; + if (a === b) return true; + return false; +} function isGreaterThan(a, b) { // your code here -}; + if (a > b) return true; + return false; +} function isLessThanOrEqualTo(a, b) { // your code here -}; + if (a <= b) return true; + return false; +} function isOdd(a) { // your code here -}; + if (a % 2 !== 0) return true; + return false; +} function isEven(a) { // your code here -}; + if (a % 2 === 0) return true; + return false; +} function isSquare(a) { // your code here -}; + if (a >= 0 && Math.sqrt(a) % 1 === 0) return true; + return false; +} function startsWith(char, string) { // your code here -}; + const text = string; + const letter = text.charAt(0); + if (char === letter) return true; + return false; +} function containsVowels(string) { // your code here -}; + let vowel = string; + vowel = vowel.match(/[aeiouAEIOU]/gi); + if (!!vowel) return true; + return false; +} function isLowerCase(string) { // your code here -}; + if (string === string.toLowerCase()) return true; + return false; +} module.exports = { negate, diff --git a/src/numbers.js b/src/numbers.js index d3eab646..43002bf4 100644 --- a/src/numbers.js +++ b/src/numbers.js @@ -1,45 +1,58 @@ 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 Math.pow(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 + const quot = ~~(a/b); + return quot; } function remainder (a, b) { // your code here + const rem = a%b; + return rem; } module.exports = { diff --git a/src/objects.js b/src/objects.js index 906eef8f..ad669663 100644 --- a/src/objects.js +++ b/src/objects.js @@ -1,41 +1,75 @@ const createPerson = (name, age) => { // your code here + const givenName = name; + const givenAge = age; + + const person = { + name: givenName, + age: givenAge + }; + return person; }; const getName = object => { // your code here + return object.name; }; const getProperty = (property, object) => { // your code here + return object[property]; }; const hasProperty = (property, object) => { // your code here + if (object[property]) { + return true; + } else { + return false; + } }; const isOver65 = person => { // your code here + if (person.age > 65) return true; + return false; }; const getAges = people => { // your code here + return people.map(person => { + return person.age; + }); }; const findByName = (name, people) => { // your code here + return people.find(people => people.name === name); }; const findHondas = cars => { // your code here + return cars.filter(cars => cars.manufacturer === 'Honda'); }; const averageAge = people => { // your code here + const total = people.reduce((prevValue, currentValue) => { + return (prevValue += currentValue.age); + }, 0); + return total / people.length; }; const createTalkingPerson = (name, age) => { // your code here + const talkingPerson = { + name, + age, + introduce: name => { + return `Hi ${name}, my name is ${talkingPerson.name} and I am ${talkingPerson.age}!`; + } + }; + return talkingPerson; }; module.exports = { diff --git a/src/strings.js b/src/strings.js index ce02affa..708a00a9 100644 --- a/src/strings.js +++ b/src/strings.js @@ -1,25 +1,40 @@ function sayHello (string) { // your code here + return 'Hello, ' + string + '!'; }; function uppercase (string) { // your code here + let result = string; + result = result.toUpperCase(); + return result; }; function lowercase (string) { // your code here + let result = string; + result = result.toLowerCase(); + return result; }; function countCharacters (string) { // your code here + let text = string; + let length = text.length; + return length; }; function firstCharacter (string) { // your code here + let text = string; + let letter = text.charAt(0); + return letter; }; function firstCharacters (string, n) { // your code here + const first = string.slice(0, n); + return first; }; module.exports = {