I’m making some functions that loop through my 2D array and check if the value in an index is matching the parameter I passed. But I keep getting this error of “TypeError: Cannot read property ‘1’ of undefined at findBookByTitle”
Any suggestions, I have tried everything 🙁
const BooksInfo = [];
function addBook(BookID, BookTitle, Author, Price, Quantity){
let newBook = [BookID,BookTitle,Author,Price,Quantity]
BooksInfo.push(newBook);
}
addBook(1,"Start with Why","Simon Sinek", 80.0, 13);
addBook(2, "But how do it know", "J. Clark Scott", 59.9, 22);
addBook(3, "Clean Code", "Rober Cecil Martin", 50.0, 5);
addBook(4, "Zero to One", "Peter Thiel", 45.0, 12);
addBook(5, "You don't know JS", "Kyle Simpson", 39.9, 9);
//console.log(BooksInfo);
function findBookByID(BookID){
for(i=0; i <= BooksInfo.length; i++){
if(BooksInfo[i][0] == BookID){
console.log(BooksInfo[i])
}
}
}
function findBookByTitle(BookTitle){
for(i=0; i <= BooksInfo.length; i++){
if(BooksInfo[i][1] == BookTitle){
console.log(BooksInfo[i])
}
}
}
function findBookByAuthor(Author){
for(i=0; i <= BooksInfo.length; i++){
if(BooksInfo[i][2] == Author){
console.log(BooksInfo[i])
}
}
}
findBookByAuthor("Kyle Simpson");
findBookByID(1);
findBookByTitle("But how do it know");