Skip to content
Open
Changes from 1 commit
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
40 changes: 20 additions & 20 deletions debugging/book-library/script.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,12 +31,21 @@ document.getElementById('submit-btn').addEventListener('click', submit);
function submit() {
const title = sanitize(titleDom.value.trim());
const author = sanitize(authorDom.value.trim());
const pages = sanitize(pagesDom.value.trim());
let pages = sanitize(pagesDom.value.trim());
if (isNaN(pages)){
alert("Enter valid value for page number!");
return false;
}
pages = parseInt(pages);
if (!isValueInteger(pages)){
alert("Enter valid value for page number!");
return false;
}

if (
title == "" ||
author == "" ||
pages == "" ||
parseInt(pages) < 0
pages < 0
) {
alert("Please fill all fields or enter valid values!");
return false;
Expand All @@ -46,17 +55,13 @@ function submit() {
render();
}
}

function isValueInteger(value) {
return Number.isInteger(value);
Copy link
Contributor

Choose a reason for hiding this comment

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

Could probably just use Number.isInteger() in the if condition directly.

}

function sanitize(string) {
const map = {
'&': '&amp;',
'<': '&lt;',
'>': '&gt;',
'"': '&quot;',
"'": '&#x27;',
"/": '&#x2F;',
};
const reg = /[&<>"'/]/ig;
return string.replace(reg, (match)=>(map[match]));
return string.replace(/[^\w\s]/gi, '');
}

function Book(title, author, pages, check) {
Expand All @@ -74,7 +79,7 @@ function render() {
//insert updated row and cells
let length = myLibrary.length;
for (let i = 0; i < length; i++) {
let row = table.insertRow(1);
let row = tableBody.insertRow(0);
let titleCell = row.insertCell(0);
let authorCell = row.insertCell(1);
let pagesCell = row.insertCell(2);
Expand All @@ -89,12 +94,7 @@ function render() {
changeBut.id = i;
changeBut.className = "btn btn-success";
wasReadCell.appendChild(changeBut);
let readStatus = "";
if (myLibrary[i].check == false) {
readStatus = "No";
} else {
readStatus = "Yes";
}
let readStatus = myLibrary[i].check ? "Yes" : "No";
changeBut.textContent = readStatus;

changeBut.addEventListener("click", function () {
Expand Down