Skip to content

Commit 6acbd7c

Browse files
committed
Prevent adding duplicate books by title
1 parent 610f0ce commit 6acbd7c

File tree

1 file changed

+9
-2
lines changed

1 file changed

+9
-2
lines changed

debugging/book-library/script.js

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,17 +37,24 @@ function submit() {
3737
alert("Please fill all fields!");
3838
return false;
3939
} else {
40+
const alreadyExists = myLibrary.some(
41+
(book) => book.title === title.value);
42+
if (alreadyExists) {
43+
alert("This book is already in your library!");
44+
return;
45+
}
46+
}
4047
let book = new Book(title.value, author.value, pages.value, check.checked);
4148
myLibrary.push(book);
4249
render();
43-
50+
4451
//clear the form after submit
4552
title.value = "";
4653
author.value = "";
4754
pages.value = "";
4855
check.checked = false;
4956
}
50-
}
57+
5158

5259
function Book(title, author, pages, check) {
5360
this.title = title;

0 commit comments

Comments
 (0)