Skip to content

Instantly share code, notes, and snippets.

@enijaward
Created May 13, 2020 17:38
Show Gist options
  • Save enijaward/a51729cf1a04bbe6fea15a12dcb408d8 to your computer and use it in GitHub Desktop.
Save enijaward/a51729cf1a04bbe6fea15a12dcb408d8 to your computer and use it in GitHub Desktop.
Assignment 3: Introduction to Javascript
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Homework 3</title>
</head>
<body>
<div>
<h1>Books Galore!</h1>
<h2>Here's what we have: </h2>
<p id="book1"></p>
<p id="book2"></p>
</div>
<script src="js/hw3.js"></script>
</body>
</html>
// creates object template
function Books(name, available, sold) {
this.name = name;
this.available = available;
this.sold = sold;
// function to subtract the amount sold from what is available
this.checkAvailability = function() {
return this.available - this.sold;
};
}
// creates object instances
var firstBook = new Books('Methods of Persuasion by Nick Kolenda', 12, 5);
var secondBook = new Books('Milk and Honey by Rupi Kaur', 20, 7);
// displays the properties, values, and method onto screen for books one and two
var productInfo = firstBook.name + ' available: ';
productInfo += firstBook.checkAvailability();
var elBook = document.getElementById('book1');
elBook.textContent = productInfo;
var productInfo = secondBook.name + ' available: ';
productInfo += secondBook.checkAvailability();
var elBook = document.getElementById('book2');
elBook.textContent = productInfo;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment