What's new

Welcome to the forum 👋, Visitor

To access the forum content and all our services, you must register or log in to the forum. Becoming a member of the forum is completely free.

My code won't output anything...

Mkk

New member
Joined
Aug 24, 2023
Messages
1
Reaction score
0
HTML Coins
0
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));
}
 

Theme customization system

You can customize some areas of the forum theme from this menu.

  • Wide/Narrow view

    You can control a structure that you can use to use your theme wide or narrow.

    Grid view forum list

    You can control the layout of the forum list in a grid or ordinary listing style structure.

    Picture grid mode

    You can control the structure where you can open/close images in the grid forum list.

    Close sidebar

    You can get rid of the crowded view in the forum by closing the sidebar.

    Fixed sidebar

    You can make it more useful and easier to access by pinning the sidebar.

    Close radius

    You can use the radius at the corners of the blocks according to your taste by closing/opening it.

  • Choose the color combination that reflects your taste
    Background images
    Color gradient backgrounds
Back