Skip to content
This repository has been archived by the owner on Jan 22, 2024. It is now read-only.

Commit

Permalink
Initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
mtslck committed Jun 13, 2017
0 parents commit 3cef98d
Show file tree
Hide file tree
Showing 5 changed files with 203 additions and 0 deletions.
37 changes: 37 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -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)
17 changes: 17 additions & 0 deletions app.js
Original file line number Diff line number Diff line change
@@ -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)
58 changes: 58 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
@@ -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": "[email protected]"
},
{
"name": "Johan Leitet",
"email": "[email protected]"
},
{
"name": "Jacob Lindehoff",
"email": "[email protected]"
},
{
"name": "Mats Loock",
"email": "[email protected]"
}
],
"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"
]
}
}
24 changes: 24 additions & 0 deletions src/my-array.js
Original file line number Diff line number Diff line change
@@ -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
67 changes: 67 additions & 0 deletions test/my-array.test.js
Original file line number Diff line number Diff line change
@@ -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()
})
})

0 comments on commit 3cef98d

Please sign in to comment.