Skip to content

Instantly share code, notes, and snippets.

@ilopX
Last active November 10, 2019 08:36
Show Gist options
  • Save ilopX/896dbc50ccf765442e333af139e55479 to your computer and use it in GitHub Desktop.
Save ilopX/896dbc50ccf765442e333af139e55479 to your computer and use it in GitHub Desktop.
function createBird() {
let distance = 0
let self = {
fly(A = 1) {
distance += A
return self
},
getDistance() {
return distance
},
compareWith(bird) {
return bird.getDistance() > distance
? 'Another bird win'
: 'I win'
}
}
return self
}
const bird1 = createBird();
const bird2 = createBird();
const bird3 = createBird();
bird1.fly()
bird1.fly()
assert(bird1.getDistance()).toBe(2)
bird2.fly().fly().fly()
assert(bird2.getDistance()).toBe(3)
bird2.fly(3)
assert(bird2.getDistance()).toBe(6)
bird1.fly(2).fly().fly(1)
assert(bird1.getDistance()).toBe(6)
bird1.fly(3)
assert(bird1.getDistance()).toBe(9)
bird3.fly(2)
assert(bird2.compareWith(bird3)).toBe('I win');
assert(bird1.compareWith(bird3)).toBe('I win');
assert(bird3.compareWith(bird1)).toBe('Another bird win');
bird3.fly(50)
assert(bird1.compareWith(bird3)).toBe('Another bird win');
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment