Skip to content

Commit ba68b00

Browse files
committed
Failure
1 parent c873e37 commit ba68b00

File tree

3 files changed

+93
-42
lines changed

3 files changed

+93
-42
lines changed

katas/potter/index.js

+25-7
Original file line numberDiff line numberDiff line change
@@ -11,22 +11,40 @@ const DISCOUNTS = [0,
1111
const BOOKS = [0,1,2,3,4]
1212

1313
function potter(books = []){
14+
const invalidBooks = books.filter( num => num < 0 && num > 4 )
15+
if (invalidBooks.length > 0){
16+
throw new Error("Invalid book number!")
17+
}
18+
1419
let totalPrice = 0
15-
let freqList = freq(books)
1620

17-
freqList.filter((f) => {
18-
return f != 0
19-
})
20-
totalPrice += BASE_PRICE * freqList.length * DISCOUNTS[freqList.length]
21-
return totalPrice
21+
for (let freqList = freq(books); freqList.length > 0; freqList = removeSet(freqList)){
22+
freqList = removeZeros(freqList)
23+
24+
totalPrice += calculateSetPrice(freqList.length)
25+
}
26+
return totalPrice
2227
}
28+
2329
function freq(books=[]){
2430
const countingBooks = [0, 0, 0, 0, 0]
2531
books.forEach((book) => {
2632
countingBooks[book] += 1
2733
})
2834
return countingBooks
2935
}
36+
37+
function removeZeros (array) {
38+
return array.filter(number => number != 0)
39+
}
3040

41+
function calculateSetPrice (setSize = 0) {
42+
return BASE_PRICE * setSize * DISCOUNTS[setSize]
43+
}
44+
45+
function removeSet(array){
46+
return array.map(x => x-1)
47+
48+
}
3149

32-
module.exports={potter, freq};
50+
module.exports={potter, freq, removeZeros, calculateSetPrice, removeSet };

katas/potter/index.test.js

+49-16
Original file line numberDiff line numberDiff line change
@@ -1,31 +1,64 @@
11
const _ = require('lodash');
2-
const {it, describe} = require('mocha');
3-
const {assert,expect} = require('chai')
2+
const { it, describe } = require('mocha');
3+
const { assert, expect } = require('chai')
44

5-
const {potter,freq} = require('./index')
5+
const { potter, freq, removeZeros, calculateSetPrice, removeSet } = require('./index')
66

77
describe('the Potter discounts', () => {
88
it('should return 0 if nothing given', () => {
99
expect(potter()).to.equal(0);
1010
});
1111

12-
it('should return the appropriate discount for different books', () =>{
12+
it('should return the appropriate discount for different books', () => {
1313
expect(potter([0])).to.equal(8);
14-
expect(potter([0,1])).to.equal(15.2);
15-
expect(potter([0,1,2])).to.equal(21.6);
16-
expect(potter([0,1,2,3])).to.equal(25.6);
17-
expect(potter([0,1,2,3,4])).to.equal(30)
18-
})
14+
expect(potter([0, 1])).to.equal(15.2);
15+
expect(potter([0, 1, 2])).to.equal(21.6);
16+
expect(potter([0, 1, 2, 3])).to.equal(25.6);
17+
expect(potter([0, 1, 2, 3, 4])).to.equal(30)
18+
});
1919

20-
it('should not apply discount for two same books',
21-
()=>{
22-
expect(potter([0,0])).to.equal(16);
23-
})
20+
it('should return the correct prices', () => {
21+
expect(potter([1, 2, 3, 1, 2, 3, 4, 3])).to.equal(55.2);
22+
expect(potter([2, 2, 2, 2, 2, 4, 3, 1, 1])).to.equal(64.8);
23+
expect(potter([0, 1, 2, 4, 2, 3, 2, 1, 0, 0])).to.equal(66.8);
24+
expect(potter([1, 1, 1, 1, 1, 1, 2])).to.equal(55.2);
25+
expect(potter([2, 3, 4, 0, 0, 1])).to.equal(38);
26+
});
27+
28+
// it('should throw an error if book number is invalid', () => {
29+
// expect(() => potter([1, 1, 1, 1, 1, 1, 6])).to.throw();
30+
// })
31+
32+
it('should not apply discount for two same books', () => {
33+
expect(potter([0, 0])).to.equal(16);
34+
})
2435
})
2536
describe('the frequence function', () => {
26-
it('should retunr the frequency function', () => {
27-
expect(freq([1,1,2,2])).to.deep.equal([0,2,2,0,0])
28-
});
37+
it('should retunr the frequency function', () => {
38+
expect(freq([1, 1, 2, 2])).to.deep.equal([0, 2, 2, 0, 0])
39+
});
2940
});
3041

42+
describe('removing zeros from frequency array', () => {
43+
it('should remove zeros from the array', () => {
44+
expect(removeZeros([0, 0, 1, 2, 1])).to.deep.equal([1, 2, 1])
45+
})
46+
})
47+
48+
describe('return the price of the set', () => {
49+
it('should calculate the price including correct discount', () => {
50+
expect(calculateSetPrice(1)).to.equal(8);
51+
expect(calculateSetPrice(2)).to.equal(15.2);
52+
expect(calculateSetPrice(3)).to.equal(21.6);
53+
expect(calculateSetPrice(4)).to.equal(25.6);
54+
expect(calculateSetPrice(5)).to.equal(30)
55+
})
56+
})
57+
58+
describe('removing the calculated set from the frequency array', () => {
59+
it('should remove 1 from each element of the array', () => {
60+
expect(removeSet([1, 2, 1])).to.deep.equal([0, 1, 0])
61+
})
62+
})
63+
3164

katas/potter/package-lock.json

+19-19
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)