diff --git a/README.md b/README.md new file mode 100644 index 0000000..f9fb3ad --- /dev/null +++ b/README.md @@ -0,0 +1,37 @@ +# Additionell kopia av array + +- A-nivå +- [GitBook](https://coursepress.gitbooks.io/1dv021/content/ovningsuppgifter/del1/additionell-kopia-av-array/) + +> __VIKTIGT!__ Innan du börjar arbeta med övningsuppgiften är det viktigt att du följer guiden [Att komma igång med en övningsuppgift](https://coursepress.gitbooks.io/1dv021/content/guider/att-komma-igang-med-en-ovningsuppgift/) för att lägga till övningsuppgiftens repo till ditt repo för övningsuppgifter. + +Hämta hem övningsuppgiftens repo och lägg till en .gitignore-fil. Öppna filen `my-array.js` och komplettera funktionen `immutablePushNumber`, som ska returnera en kopia av en array där ett tal lagts till. Funktionen ska vara en så kallad "_pure function_", d.v.s. vara helt utan sidoeffekter. + +Funktionen `immutablePushNumber` har två parametrar. Den första parametern är den array som ska kopieras. Den andra parameter är det tal som ska läggas till kopian av arrayen. + +Du ska även implementera viss felhantering, innebärande att undantag kastas om fel inträffar. Om första parametern inte är av typen `Array` eller om andra parametern inte är av typen `Number` ska ett undantag av typen `TypeError` kastas. + +```js +const ma = require('./src/my-array'); +let arr = [1, 2, 3]; +let newArray = ma.immutablePushNumber(arr, 4); // newArray must be [1, 2, 3, 4] + // arr must be unchanged, i.e. arr !== newArray +``` + +## Tips + +Genom att köra testerna som kommer med övningsuppgiften kan du undersöka om koden du skrivit löst uppgiften (i alla fall enligt testet...). + +Funktioner, metoder, etc. som _kan_ komma till användning beroende hur du väljer att lösa uppgiften. + +- [if](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/if...else) +- [Array.isArray](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/isArray) +- [Array.length](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/length) +- [Array.slice](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/slice) +- [Array.push](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/push) +- [throw](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/throw) +- [TypeError](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/TypeError) + +## Lösningsförslag + +- [https://github.com/1dv021/exercise-solution-proposals/tree/master/part-1/additional-array-copy](https://github.com/1dv021/exercise-solution-proposals/tree/master/part-1/additional-array-copy) diff --git a/app.js b/app.js new file mode 100644 index 0000000..c76a9d2 --- /dev/null +++ b/app.js @@ -0,0 +1,17 @@ +/** + * The starting point of the application. + * + * @author Mats Loock + * @version 1.1.0 + */ + +'use strict' + +const ma = require('./src/my-array.js') + +let arr = [1, 2, 3] +let newArr = ma.immutablePushNumber(arr, 4) + +console.log(arr) +console.log(newArr) +console.log(arr !== newArr) diff --git a/package.json b/package.json new file mode 100644 index 0000000..dc56bd4 --- /dev/null +++ b/package.json @@ -0,0 +1,58 @@ +{ + "name": "exercise-additional-array-copy", + "version": "1.1.0", + "main": "app.js", + "homepage": "http://coursepress.lnu.se/kurs/grundlaggande-programmering/", + "contributors": [ + { + "name": "John Häggerud", + "email": "john.haggerud@lnu.se" + }, + { + "name": "Johan Leitet", + "email": "johan.leitet@lnu.se" + }, + { + "name": "Jacob Lindehoff", + "email": "jacob.lindehoff@lnu.se" + }, + { + "name": "Mats Loock", + "email": "mats.loock@lnu.se" + } + ], + "license": "CC-BY-4.0", + "repository": { + "type": "git", + "url": "https://github.com/1dv021/exercise-additional-array-copy.git" + }, + "engines": { + "node": ">=0.12.0" + }, + "dependencies": {}, + "devDependencies": { + "chai": "^3.5.0", + "mocha": "^3.3.0", + "snazzy": "^7.0.0", + "standard": "*" + }, + "directories": { + "test": "test" + }, + "scripts": { + "start": "node app.js", + "test": "standard | snazzy && mocha --recursive ./test" + }, + "standard": { + "globals": [ + "describe", + "context", + "before", + "beforeEach", + "after", + "afterEach", + "it", + "expect" + ] + } +} diff --git a/src/my-array.js b/src/my-array.js new file mode 100644 index 0000000..bde1a15 --- /dev/null +++ b/src/my-array.js @@ -0,0 +1,24 @@ +/** + * My-array module. + * + * @module src/my-array + * @author John Häggerud + * @author Mats Loock + * @version 1.1.0 + */ + +'use strict' + +/** + * Returns a copy of an array where a number has been added to the end of the copy. + * + * @param {Array} source The array to create a copy of. + * @param {Number} number The number to add to the end of the copy of the array. + * @throws {TypeError} The source parameter must be an Array; number parameter must be a Number. + * @returns {Array} A copy of the source array with an additional number. + */ +function immutablePushNumber (source, number) { + // TODO: Write your code here. +} + +exports.immutablePushNumber = immutablePushNumber diff --git a/test/my-array.test.js b/test/my-array.test.js new file mode 100644 index 0000000..110f4bc --- /dev/null +++ b/test/my-array.test.js @@ -0,0 +1,67 @@ +/** + * Tests for the my-array module. + * + * @author John Häggerud + * @author Mats Loock + * @version 1.1.0 + */ + +'use strict' + +const ma = require('../src/my-array') +const expect = require('chai').expect + +describe('Test error handling', () => { + it('Must throw a TypeError if no parameter is provided.', done => { + expect(() => { ma.immutablePushNumber() }).to.throw(TypeError) + done() + }) + + it('Must throw a TypeError if second parameter is not provided.', done => { + expect(() => { ma.immutablePushNumber([1]) }).to.throw(TypeError) + done() + }) + + it('Must throw a TypeError if the source parameter is not an Array.', done => { + expect(() => { ma.immutablePushNumber('not an array', 1) }).to.throw(TypeError) + done() + }) + + it('Must throw a TypeError if the number parameter is not a Number.', done => { + expect(() => { ma.immutablePushNumber([], 'not a number') }).to.throw(TypeError) + done() + }) +}) + +describe('Test that the source array is untouched.', () => { + it('Must NOT return the same Array object the source parameter refers to.' + + '(don\'t forget to make a copy of the source array).', done => { + let arr = [1, 2, 3] + let res = ma.immutablePushNumber(arr, 4) + expect(arr).to.not.eql(res) + done() + }) +}) + +describe('Test that the new array contains the provided number', () => { + it('Must return a new copy of the source with the the additional number at the end.', done => { + let arr = [1, 2, 3] + let res = ma.immutablePushNumber(arr, 4) + expect(res).to.eql([1, 2, 3, 4]) + done() + }) + + it('Must return a new copy of the source, an array, with the the additional number at the end.', done => { + let arr = [1] + let res = ma.immutablePushNumber(arr, 4) + expect(res).to.eql([1, 4]) + done() + }) + + it('Must return a new copy of the source, an array, with the the additional number at the end.', done => { + let arr = [] + let res = ma.immutablePushNumber(arr, 4) + expect(res).to.eql([4]) + done() + }) +})