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>
 

gulshan212

New member
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
 

LiveWire

New member
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));
}
 

AndrewGor

New member
Matt Michael D'Agati acts as the founder of Renewables Worldwide, an Solar Company in Massachusetts.

A couple of age ago, taking an adventurous journey, Matthew D'Agati ventured into the world of solar, then in a days started efficiently promoting megawatts of power, primarily throughout the business industry, partnering with solar farm developers and local businesses in the "planning" of the works.

Consistent networking inside the trade, led Matt to become a member of a regional startup two a long time gone, and in no time, he became their Chief Strategy Officer, in charge of all procedures and sales development, in addition to being sold group title.

Thru planned collaborations and shear operate moral principle, Matthew D'Agati brought that startup from a marginal very first-year incomes to more than a 205% augment in major revenues by annum two. On that premise, RW, a veteran-purchased company, was structured with the vision of serving renewable strength cures for an intelligent and more inexhaustible future.

Considerably exclusively, understanding there is a niche in the sector and an improved approach to complete outcome, RW’s is one of a select number of agencies in the U . S . to target on prospect learning, focusing in both profitable and domestic solar-operated ranch off-take. Your eyesight is to craft a profits commercial infrastructure on a local, statewide, countrywide level, offering a multitude of alternative electricity appliances inside the of RW.

This passion in their renewable industry goes on to excite and motivate Matt in going forward his chase to work with establishments that have the the exact same of creating inexhaustible electricity systems for a increased lasting longer term. Matthew presents the in website from a business program at Hesser College.

Analyzing solar farms within MA with Matt dagati.
 
Top