Skip to content

Commit 3364cc4

Browse files
committed
added string enums to solution
1 parent 6f6469e commit 3364cc4

File tree

2 files changed

+20
-14
lines changed

2 files changed

+20
-14
lines changed

exercises/two-bucket/two-bucket.example.ts

+14-8
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,18 @@
1-
export default class TwoBucket {
2-
starter: string
1+
2+
export enum Bucket {
3+
One = "one",
4+
Two = "two"
5+
}
6+
7+
export class TwoBucket {
8+
starter: Bucket
39
x: number
410
y: number
511
z: number
6-
goalBucket: string
12+
goalBucket: Bucket
713
otherBucket: number
814

9-
constructor(x: number, y: number, z: number, starter: string) {
15+
constructor(x: number, y: number, z: number, starter: Bucket) {
1016
this.starter = starter
1117
this.x = x
1218
this.y = y
@@ -15,10 +21,10 @@ export default class TwoBucket {
1521
reachedGoal(measurements: number[]): boolean {
1622
if (measurements[0] === this.z || measurements[1] === this.z) {
1723
if (measurements[0] === this.z) {
18-
this.goalBucket = 'one'
24+
this.goalBucket = Bucket.One
1925
this.otherBucket = measurements[1]
2026
} else {
21-
this.goalBucket = 'two'
27+
this.goalBucket = Bucket.Two
2228
this.otherBucket = measurements[0]
2329
}
2430
return true
@@ -77,11 +83,11 @@ export default class TwoBucket {
7783
moves() {
7884
let j = 0,
7985
k = 0 // j will be running val of bucket one, k = running val of bucket two
80-
this.starter === 'one' ? j = this.x : k = this.y
86+
this.starter === Bucket.One ? j = this.x : k = this.y
8187
const measurements = [j, k]
8288
let moveCount = 0
8389
const prBool = true // pour / receive boolean - need to pour or receive every other turn
84-
if (this.starter === 'one') {
90+
if (this.starter === Bucket.One) {
8591
moveCount = this.smallFirst(measurements, moveCount, prBool)
8692
} else {
8793
moveCount = this.bigFirst(measurements, moveCount, prBool)

exercises/two-bucket/two-bucket.test.ts

+6-6
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import TwoBucket from './two-bucket';
1+
import {TwoBucket, Bucket} from './two-bucket'
22

33
describe('TwoBucket', () => {
44
describe('works for input of 3, 5, 1', () => {
@@ -7,15 +7,15 @@ describe('TwoBucket', () => {
77
const goal = 1
88

99
test('starting with bucket one', () => {
10-
const starterBuck = 'one'; // indicates which bucket to fill first
10+
const starterBuck = Bucket.One // indicates which bucket to fill first
1111
const twoBucket = new TwoBucket(buckOne, buckTwo, goal, starterBuck)
1212
expect(twoBucket.moves()).toEqual(4) // includes the first fill
1313
expect(twoBucket.goalBucket).toEqual('one') // which bucket should end up with the desired # of liters
1414
expect(twoBucket.otherBucket).toEqual(5) // leftover value in the "other" bucket once the goal has been reached
15-
});
15+
})
1616

1717
xtest('starting with bucket two', () => {
18-
const starterBuck = 'two'
18+
const starterBuck = Bucket.Two
1919
const twoBucket = new TwoBucket(buckOne, buckTwo, goal, starterBuck)
2020
expect(twoBucket.moves()).toEqual(8)
2121
expect(twoBucket.goalBucket).toEqual('two')
@@ -29,15 +29,15 @@ describe('TwoBucket', () => {
2929
const goal = 2
3030

3131
xtest('starting with bucket one', () => {
32-
const starterBuck = 'one'
32+
const starterBuck = Bucket.One
3333
const twoBucket = new TwoBucket(buckOne, buckTwo, goal, starterBuck)
3434
expect(twoBucket.moves()).toEqual(14)
3535
expect(twoBucket.goalBucket).toEqual('one')
3636
expect(twoBucket.otherBucket).toEqual(11)
3737
})
3838

3939
xtest('starting with bucket two', () => {
40-
const starterBuck = 'two'
40+
const starterBuck = Bucket.Two
4141
const twoBucket = new TwoBucket(buckOne, buckTwo, goal, starterBuck)
4242
expect(twoBucket.moves()).toEqual(18)
4343
expect(twoBucket.goalBucket).toEqual('two')

0 commit comments

Comments
 (0)