Javascript#1
Conversation
Krishan-Mishra
commented
Sep 13, 2023
- Added the generate book data code
- Added the promise fetch functionality to render the data in frontend including all search implmentation
| @@ -0,0 +1 @@ | |||
| node_modules/ No newline at end of file | |||
| function IdButton(event) { | ||
| event.preventDefault(); | ||
| const idValue = document.getElementById("input-search-id").value; | ||
| if (idValue.trim().length <= 0) return; |
There was a problem hiding this comment.
I am not sure why length will go less than 0.
The above line can be written as -
if (i!dValue.trim().length) return;
Please validate.
| }) | ||
| .catch((error) => console.log(error)); | ||
|
|
||
| document.getElementById("btn-search-id").onclick = IdButton; |
There was a problem hiding this comment.
Read about how onClick event is added in ES6.
The above line can be written as -
document.getElementById('btn-search-id').addEventListener('click', IdButton);
The function naming is not self explanatory. What is the work of IdButton?
There was a problem hiding this comment.
Similarly there are other instances where improvement is needed.
| function genreButton(event) { | ||
| event.preventDefault(); | ||
| const genreValue = document.getElementById("input-search-genre").value; | ||
| if (genreValue.trim().length <= 0) return; |
| id="div-search-price" | ||
| class="container" | ||
| style="margin-bottom: 20px; padding-top: 20px" | ||
| > |
There was a problem hiding this comment.
Don't use an extra line for this closing bracket. Add it in the line 20 itself. Implement it at all the instances
|
|
||
| const bookList = new Array(); | ||
|
|
||
| for (let i = 1; i <= 100; i++) { |
There was a problem hiding this comment.
Use some more meaningful name instead of "i" like index or something
| @@ -0,0 +1 @@ | |||
| node_modules/ No newline at end of file | |||
| document.getElementById("btn-search-genre").onclick = genreButton; | ||
| document.getElementById("btn-search-price").onclick = priceButton; | ||
|
|
||
| function IdButton(event) { |
| } | ||
| } | ||
|
|
||
| function generateRandomBook(bookId) { |
There was a problem hiding this comment.
add this method to inside in class
| @@ -0,0 +1,43 @@ | |||
| const fs = require("fs"); | |||
|
|
|||
| </head> | ||
|
|
||
| <body> | ||
| <div |
| <form action="#"> | ||
| <input | ||
| type="text" | ||
| id="input-search-id" |
There was a problem hiding this comment.
don't use id instead use class
| type="text" | ||
| id="input-search-genre" | ||
| class="form-control" | ||
| style="width: 25%; display: inline" |
08c53d7 to
0fc6f36
Compare
0fc6f36 to
bc5a6a9
Compare
| @@ -0,0 +1,66 @@ | |||
| let BookData = []; | |||
There was a problem hiding this comment.
This is usually a way to name the class. Variable name should be - let bookData = [];
| insertDataInHtml(matchingBooks); | ||
| }; | ||
|
|
||
| const searchByGenreButton = (event) => { |
There was a problem hiding this comment.
Instead of event use evt.
event is not a reserved keyword, but it is a global variable in IE at least.
| const price = faker.finance.amount(5, 30, 2); | ||
| this.bookId = bookId; | ||
| this.genre = genre; | ||
| this.price = price; |
There was a problem hiding this comment.
Instead of defining a price variable you can directly used as follows -
this.price = faker.finance.amount(5, 30, 2);
4a507cd to
0e10b0c
Compare