Skip to content

Animal shelter #28

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 38 additions & 0 deletions javascript/code-challenges/animal-shelter/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
# Challenge Summary

## AnimalShelter
- Create a class called AnimalShelter which holds only dogs and cats.
- The shelter operates using a first-in, first-out approach.
-

### Required Methods
#### `enqueue`

- Arguments: animal
- animal can be either a dog or a cat object

#### `dequeue`

- Arguments: pref
- pref can be either "dog" or "cat"
- Return: either a dog or a cat, based on preference.
- If pref is not "dog" or "cat" then return null.

## Approach & Efficiency

Use an object for storage

| Method | Time Complexity | Space Complexity |
| ---- | ---- | ---- |
| enqueue | O(1) | O(1)|
| dequeue | O(n) | O(1) |


```Bash
# run all related tests
npm test animal-shelter.test.js
```

### Sources

[ Implement a Queue in JavaScript ](https://initjs.org/implement-a-queue-in-javascript-de306a8821c)
43 changes: 43 additions & 0 deletions javascript/code-challenges/animal-shelter/animal-shelter.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
'use strict'

class AnimalShelter{
constructor(storage = {}, front = 0, back = 0) {
this.storage = storage
this.front = front
this.back = back
}

// enqueue
enqueue(animalObject) {
this.back++
this.storage[this.back] = animalObject
}

// dequeue
dequeue(pref = null) {
if(this.back === 0){throw new Error('Shelter is empty')}
let result
if(pref === null) {
Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should return null if pref in null instead of next in queue.

result = this.storage[this.front]
delete this.storage[this.front]
this.front++
return result
}
let tempCounter = +this.front
while(this.storage[`${tempCounter}`].type !== pref && tempCounter < this.back) {
let gapCounter = 1
while(this.storage[`${tempCounter + gapCounter}`] === undefined && gapCounter < this.back) {
gapCounter++
}
tempCounter+= gapCounter
}
result = this.storage[`${tempCounter}`]
delete this.storage[tempCounter]
if(tempCounter === this.front) {
this.front++
}
return result
}
}

module.exports = AnimalShelter
72 changes: 72 additions & 0 deletions javascript/code-challenges/animal-shelter/animal-shelter.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
'use strict'

const AnimalShelter = require('./animal-shelter')

let populatedAnimalShelter, emptyAnimalShelter

beforeEach(() => {
populatedAnimalShelter = new AnimalShelter({
2: {
type: 'dog',
name: 'Fluff'
},
4: {
type: 'cat',
name: 'Mittens'
},
5: {
type: 'dog',
name: 'Boscow'
}
}, 2, 5)
emptyAnimalShelter = new AnimalShelter()
})

describe('Given Animal Shelter', () => {
describe('When enqueue', () => {
let animalToAdd = {
type: 'cat',
name: 'Smittens'
}
it('Then should be able to add an animal populated shelter', () => {
populatedAnimalShelter.enqueue(animalToAdd)
expect(populatedAnimalShelter.storage).toStrictEqual({
'2': { type: 'dog', name: 'Fluff' },
'4': { type: 'cat', name: 'Mittens' },
'5': { type: 'dog', name: 'Boscow' },
'6': { type: 'cat', name: 'Smittens' }
})
})

it('Then should be able to add animal to empty shelter', () => {
emptyAnimalShelter.enqueue(animalToAdd)
expect(emptyAnimalShelter.storage).toStrictEqual({
'1': { type: 'cat', name: 'Smittens' }
})
})
})

describe('When dequeue', () => {
it('Then should be able to dequeue animal based on preference', () => {
populatedAnimalShelter.dequeue('cat')
expect(populatedAnimalShelter.storage).toStrictEqual({
'2': { type: 'dog', name: 'Fluff' },
'5': { type: 'dog', name: 'Boscow' },
})
})

it('Then should dequeue animal there the longest if no preference provided', () =>{
populatedAnimalShelter.dequeue()
expect(populatedAnimalShelter.storage).toStrictEqual(
{
'4': { type: 'cat', name: 'Mittens' },
'5': { type: 'dog', name: 'Boscow' }
}
)
})

it('Then should throw error on empty shelter', () => {
expect(() => {emptyAnimalShelter.dequeue()}).toThrow('Shelter is empty')
})
})
})