Skip to content
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
61 changes: 61 additions & 0 deletions challenges/bookingManagement/BookManager.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
class BookManager {
constructor() {
this.books = [];
}

createBook(idArg, title) {
const id = parseInt(idArg, 10);
const foundBook = this.books.find((book) => book.id === id);
if (!foundBook) {
const book = {};
book.id = id;
book.title = title;
this.books = [...this.books, book];
return true;
}
return false;
}

updateBook(idArg, title) {
const id = parseInt(idArg, 10);
const foundBook = this.books.find((book) => book.id === id);
if (!foundBook) {
return false;
}
const foundBookIndex = this.books.indexOf(foundBook);

const book = {};
book.id = id;
book.title = title;

const currentBookList = this.books;
currentBookList.splice(foundBookIndex, 1, book);
this.books = [...currentBookList];
return true;
}

deleteBook(idArg) {
const id = parseInt(idArg, 10);
const foundBook = this.books.find((book) => book.id === id);
const foundBookIndex = this.books.indexOf(foundBook);
if (!foundBook) {
return false;
}
const newBookArray = this.books.splice(foundBookIndex, 1);
this.books = newBookArray;
return true;
}

findBookById(idArg) {
const id = parseInt(idArg, 10);
const foundBook = this.books.find((book) => book.id === id);
return foundBook || null;
}

findBookByTitle(title) {
const foundBook = this.books.find((book) => book.title === title);
return foundBook || null;
}
}

module.exports = BookManager;
157 changes: 157 additions & 0 deletions challenges/bookingManagement/bookingManagement.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,157 @@
const BookManager = require('./BookManager');

// Do NOT edit the code below this comment.
// You should be able to complete this test without editing below this comment.

function bookManagementRefactor(operations) {
const bookManager = new BookManager();
// Calls corresponding methods of bookManager based on the input
return operations.map((operation) => {
const [methodName, ...params] = operation;
const result = bookManager[methodName].call(bookManager, ...params);
return (result === undefined || result === null) ? null : JSON.stringify(result);
});
}

describe('bookingManagement createBook Test', () => {
test('testOne', () => {
const bookManager = new BookManager();
bookManager.createBook('10', 'Book_10');
const result = bookManager.books;
const answer = [{ id: 10, title: 'Book_10' }];
expect(result).toEqual(answer);
});
});

describe('bookingManagement updateBook Test', () => {
test('testOne', () => {
const bookManager = new BookManager();
bookManager.createBook('10', 'Book_10');
bookManager.updateBook('10', 'Updated_Book_10');
const result = bookManager.books;
const answer = [{ id: 10, title: 'Updated_Book_10' }];
expect(result).toEqual(answer);
});
});

describe('bookingManagement Test', () => {
test('testOne', () => {
const testOne = {
input: [['createBook', '10', 'Book_10'],
['createBook', '10', 'Book_10'],
['updateBook', '10', 'New_Book_10'],
['deleteBook', '9'],
['findBookById', '9'],
['findBookById', '10'],
['findBookByTitle', 'Book_10'],
['findBookByTitle', 'New_Book_10']],
output: ['true',
'false',
'true',
'false',
null,
'{"id":10,"title":"New_Book_10"}',
null,
'{"id":10,"title":"New_Book_10"}'],
};
const result = bookManagementRefactor(testOne.input);
expect(result).toEqual(testOne.output);
});

test('testTwo', () => {
const testOne = {
input: [['createBook', '10', 'Book 10']],
output: ['true'],
};

const result = bookManagementRefactor(testOne.input);
expect(result).toEqual(testOne.output);
});

test('testThree', () => {
const testOne = {
input: [['updateBook', '462', 'Book 462']],
output: ['false'],
};

const result = bookManagementRefactor(testOne.input);
expect(result).toEqual(testOne.output);
});


test('testFour', () => {
const testOne = {
input: [['createBook', '23', 'The Greeen Book'],
['createBook', '23', 'The Red Book'],
['createBook', '22', 'The Green Book'],
['updateBook', '23', 'The Green Book'],
['findBookById', '23'],
['findBookById', '22'],
['updateBook', '22', 'The Red Book'],
['findBookByTitle', 'The Red Book'],
['findBookByTitle', 'The Green Book']],
output: ['true',
'false',
'true',
'true',
'{"id":23,"title":"The Green Book"}',
'{"id":22,"title":"The Green Book"}',
'true',
'{"id":22,"title":"The Red Book"}',
'{"id":23,"title":"The Green Book"}'],
};

const result = bookManagementRefactor(testOne.input);
expect(result).toEqual(testOne.output);
});

test('testFive', () => {
const testOne = {
input: [['createBook', '733', 'Book 733'],
['findBookById', '236']],
output: ['true',
null],
};

const result = bookManagementRefactor(testOne.input);
expect(result).toEqual(testOne.output);
});

test('testSix', () => {
const testOne = {
input: [['createBook', '733', 'Book 733'],
['findBookById', '236']],
output: ['true',
null],
};

const result = bookManagementRefactor(testOne.input);
expect(result).toEqual(testOne.output);
});

test('testSeven', () => {
const testOne = {
input: [['createBook', '733', 'Book 733'],
['findBookById', '733']],
output: ['true',
'{"id":733,"title":"Book 733"}'],
};

const result = bookManagementRefactor(testOne.input);
expect(result).toEqual(testOne.output);
});

test('testEight', () => {
const testOne = {
input: [['createBook', '269', 'Book 269'],
['updateBook', '454', 'Book 454'],
['findBookByTitle', 'Book 269']],
output: ['true',
'false',
'{"id":269,"title":"Book 269"}'],
};

const result = bookManagementRefactor(testOne.input);
expect(result).toEqual(testOne.output);
});
});
124 changes: 124 additions & 0 deletions challenges/bookingManagement/bookingManagementNotes.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,124 @@
bookingManagement Notes go here!


/////////////////////
function BookManager() {
this.books = [];

this.createBook = function (id, title) {
// TODO: return false if the book id already exists

id = parseInt(id);
let book = new Object();
book.id = id;
book.title = title;
this.books = [...this.books, book];
return true;
};

this.updateBook = function (id, title) {
// TODO: return false if the book doesn't exist

id = parseInt(id);
this.books = this.books.filter(book => {
return {
id: id,
title: title
};
});
return true;
};

this.deleteBook = function (id) {
// TODO: return false if the book doesn't exist

id = parseInt(id);
const book = this.books.find(book => book.id === id);
delete book;
return true;
};

this.findBookById = function (id) {
return this.books.find(book => book.id === parseInt(id));
};

this.findBookByTitle = function (title) {
return this.books.find(book => book.title === title);
};
}

// Do NOT edit the code below this comment.
// You should be able to complete this test without editing below this comment.

const bookManager = new BookManager();

function bookManagementRefactor(operations) {
// Calls corresponding methods of bookManager based on the input
return operations.map(operation => {
const [methodName, ...params] = operation;
let result = bookManager[methodName].call(bookManager, ...params);
return result === undefined ? "null" : JSON.stringify(result);
});
}


//////////////FINAL
function BookManager() {
this.books = {};

this.createBook = function (id, title) {
idl = parseInt(id); /*book id*/
let bookCreated = false

if(!this.books.hasOwnProperty(idl)){
let book = new Object();
book.id = idl;
book.title = title;
this.books[idl] = book
bookCreated = true
}
return bookCreated
};

this.updateBook = function (id, title) {
id = parseInt(id);
let bookUpdated = false;
if(this.books[id]){
this.books[id]['title'] = title
bookUpdated = true
}
return bookUpdated;
};

this.deleteBook = function (id) {
id = parseInt(id);
let bookDeleted = false
if(this.books[id]){
delete this.books[id]
bookDeleted = true
}
return bookDeleted;
};

this.findBookById = function (id) {
return this.books[id] ? this.books[id] : null
};

this.findBookByTitle = function (title) {
return Object.values(this.books).find(book => book.title === title);
};
}

// Do NOT edit the code below this comment.
// You should be able to complete this test without editing below this comment.

const bookManager = new BookManager();

function bookManagementRefactor(operations) {
// Calls corresponding methods of bookManager based on the input
return operations.map(operation => {
const [methodName, ...params] = operation;
let result = bookManager[methodName].call(bookManager, ...params);
return result === undefined ? "null" : JSON.stringify(result);
});
}
45 changes: 45 additions & 0 deletions challenges/bookingManagement/bookingManagementSpec.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
bookingManagement Spec go here!

You are given a class that handles operations with books, however it is not complete and might have some issues, your task is to refactor it, fix the issues and add the missing parts.

Each book is described by 2 fields: id and title.

The methods that should be supported are listed below:

createBook(id, title) - creates new book. Returns false if the book with such id already exists, and true otherwise;
updateBook(id, title) - updates provided book. Returns false if the book with such id does not exist, and true otherwise.
deleteBook(id) - deletes provided book. Returns false if the book with such id does not exist, and true otherwise.
findBookById(id) - find book by id. Returns the book, if the book with such id exists, and null otherwise.
findBookByTitle(title) - find book by title. Returns the book, if the book with such title exists, and null otherwise. It is guaranteed that at any time there are no two books with the same title.
Implement all these methods.

Example

For operations = [["createBook", "10", "Book_10"], ["createBook", "10", "Book_10"], ["updateBook", "10", "New_Book_10"], ["deleteBook", "9"],["findBookById", "9"], ["findBookById", "10"], ["findBookByTitle", "Book_10"], ["findBookByTitle", "New_Book_10"]], the output should be
bookManagementRefactor(operations) = ["true", "false", "true", "false", "null", "{"id":10,"title":"New_Book_10"}", "null", "{"id":10,"title":"New_Book_10"}"].

//////////////////////////NOTHING IMPORTANT

Input/Output

[execution time limit] 4 seconds (js)

[input] array.array.string operations

An array of operations desribed above.

Guaranteed constraints:
1 ≤ operations.length ≤ 103,
2 ≤ operations[i].length ≤ 3.

[output] array.string

An array with ith element equal to the result of the ith operation.
[JavaScript (ES6)] Syntax Tips

// Prints help message to the console
// Returns a string
function helloWorld(name) {
console.log("This prints to the console when you Run Tests");
return "Hello, " + name;
}
5 changes: 5 additions & 0 deletions challenges/dataWork/dataWork.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
function dataWork(input) {

}

module.exports = dataWork;
Loading