What's new
HTML Forums | An HTML and CSS Coding Community

Welcome to HTMLForums; home of web development discussion! Please sign in or register your free account to get involved. Once registered you will be able to connect with other members, send and receive private messages, reply to topics and create your very own. Our registration process is hassle-free and takes no time at all!

My code won't output anything...

Mkk

New member
I'm trying to make my code output something like this:

orange: 4.1
apple: 3.84
melon: 3.06
subtotal: 11
tax: 0.88
total: 11.88

but It wont output anything

here it is:
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>Basic Page</title>
</head>
<body>
<script>
var cart = [] //creates an empty array for now

function createGroceryItem(name, price, quantity) {
var grocery = {"name" : name, "price" : price, "quantity": quantity, "totalPrice": function() { return price * quantity }, "printOut" : printOut};
return grocery;
}

function printOut() {
console.log(this.name + " #" + this.quantity + " : " + this.totalPrice());
}

function printReceipt(item1, item2, item3){
var subtotal =
item1.totalPrice();
item2.totalPrice(),
item3.totalPrice();
var tax = subtotal*.08;
var total = subtotal + tax;
item1.printOut();
item2.printOut();
item3.printOut();
console.log("subtotal:" + subtotal + "tax:" + tax + "ntotal:" + total);
}

cart.push(createGroceryItem("oranges", .82, 5));
cart.push(createGroceryItem("apples", .64, 6));
cart.push(createGroceryItem("melons", 1.02, 3));

printReceipt(cart[0],cart[1],cart[2]);

</script>
</body>
</html>
 
Well, there are some logical errors with your code, you can try below code to fix this error.

<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>Basic Page</title>
</head>
<body>
<script>
var cart = [];

function createGroceryItem(name, price, quantity) {
var grocery = {
"name": name,
"price": price,
"quantity": quantity,
"totalPrice": function() {
return price * quantity;
},
"printOut": printOut
};
return grocery;
}

function printOut() {
console.log(this.name + ": " + this.totalPrice().toFixed(2));
}

function printReceipt(item1, item2, item3) {
var subtotal = item1.totalPrice() + item2.totalPrice() + item3.totalPrice();
var tax = subtotal * 0.08;
var total = subtotal + tax;

item1.printOut();
item2.printOut();
item3.printOut();
console.log("subtotal: " + subtotal.toFixed(2));
console.log("tax: " + tax.toFixed(2));
console.log("total: " + total.toFixed(2));
}

cart.push(createGroceryItem("orange", 0.82, 5));
cart.push(createGroceryItem("apple", 0.64, 6));
cart.push(createGroceryItem("melon", 1.02, 3));

printReceipt(cart[0], cart[1], cart[2]);
</script>
</body>
</html>

Thanks
 
The issue is in the printReceipt function. You are trying to calculate the subtotal but due to the usage of commas instead of addition (+), only the last value (i.e., item3.totalPrice()) is being assigned to the subtotal variable. This makes your subtotal incorrect

function printReceipt(item1, item2, item3) {
var subtotal = item1.totalPrice() + item2.totalPrice() + item3.totalPrice();
var tax = subtotal * 0.08;
var total = subtotal + tax;
item1.printOut();
item2.printOut();
item3.printOut();
console.log("subtotal: " + subtotal);
console.log("tax: " + tax);
console.log("total: " + total);
}

another small correction is in the printOut function. You have mentioned # before the quantity, but the desired output doesn't contain #. Here's the corrected function:

function printOut() {
console.log(this.name + ": " + this.totalPrice().toFixed(2));
}
 
Back
Top