Loops, Arrays and Objects. Reading List Challenge

Hi, I am new to javascript and I am really struggling with this challenge I am doing as part of a beginners course I am attempting. Was wondering if anyone could give me some help and pointers on where I am going wrong.
I am trying to create a loop that goes through the array I have created and produce a statement declaring whether I have or have not read a certain book. I have managed to get the final Array to pull through but the console is not logging any of my other variations and I don’t know why. Thank you for reading this and your help is greatly appreciated.

Here is my code so far;

<!DOCTYPE html>
<html>
	<head>
		<title> Books to read</title>
	</head>
	<body>
		<script>

			var books = [
			{title: 'Game of Thrones', author: 'George R.R. Martin', alreadyRead: "Yes"},
			{title: 'Alice in Wonderland', author: 'C.S. Lewis', alreadyRead:"Yes"},
			{title: 'The Nightlords Omnibus', author: 'Aaron Dembski-Bowden', alreadyRead: "No"},
			{title: 'Harry Potter', author: 'J.K Rowling', alreadyRead: "No"}
			]
			for (var i = 0; i < books.length; i++) {
			  var book= books[i];
			} if (book.alreadyRead == "Yes"){
				console.log ("You have already read "+book.title+" by "+book.author);
			} else if(book.alreadyRead == "No") {
				console.log("You have not read "+book.title+" by "+book.author); 
			}


		</script>

	</body>
</html>
for (var i = 0; i < books.length; i++) {
  var book= books[i];
} if (book.alreadyRead == "Yes"){
  console.log ("You have already read "+book.title+" by "+book.author);
} else if(book.alreadyRead == "No") {
  console.log("You have not read "+book.title+" by "+book.author); 
}

This here… Can we agree that the if/else block is outside of the for loop? Should it be?

You are absolutely right, syntax has been the hardest thing for me with Javascript. Thank you. That sorted it, as I am now actually putting all the array through the function.

This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.