for(var i=0; i<5; i++) {
inputArray[i] = prompt("Input book title, author, price with ,");
function Book(title, author, price) {
this.title = title;
this.author = author;
this.price = price;
}
var bookAfter = inputArray[i].split(",");
var myBook = new Book(bookAfter[0], bookAfter[1], parseInt(bookAfter[2]));
bookArray.push(myBook);
}
// Finding max price
for(var price in bookArray) {
var first = bookArray[0];
console.log(first);
if(first[price] < bookArray[i+1][price]) {
first = bookArray[i+1];
}
}
Input information is like this
A, John, 10
B, Ashley, 15
C, James, 7
D, Emily, 9
E, George, 12
The problem is when I console.log(first.price)
, console.log(first[price])
or console.log(bookArray[3].price)
I can get the right value of the price for example 10
.
But the if statement gets error with this message Uncaught TypeError: Cannot read property 'price' of undefined
when using if(first.price < bookArray[i+1].price)
and
gets another message Uncaught TypeError: Cannot read property '0' of undefined
when using if(first.price < bookArray[i+1].price)
The basic for loop like for(var i=0; i<5; i++)
doesn't work as well.
Any help would be appreciated :)