JS calculates the total price of goods in the shopping cart for your reference. The specific contents are as follows Question requirements:There is information about several products in the shopping cart, including the name, unit price, and quantity of the products. The total price of the products in the shopping cart is calculated. Specific ideas:Product information is obtained by creating product objects. The sum of several products is achieved by creating an array to place the products. The price is then calculated by traversing the array and reading the specified attributes. Specific code:<script type="text/javascript"> // Total price variable var sum = 0; // Goods object function Goods(name,price,amount){ this.name = name; this.price = price; this.amount = amount; // this.add = fun(); } //Define and declare a product instance var goods1 = new Goods("Pen",100,1); var goods2 = new Goods("paper towel",10,1); var goods3 = new Goods("Workbook",100,2); // Create a function to calculate the total price function totalPrice(){ // Put objects into array var arr = new Array(goods1,goods2,goods3); // Add the prices of each product by traversing for(var i in arr){ sum = sum + (arr[i].price * arr[i].amount); }; console.log(sum); }; console.log(goods1); console.log(goods2); console.log(goods3); totalPrice(); </script> Running results: The above is the full content of this article. I hope it will be helpful for everyone’s study. I also hope that everyone will support 123WORDPRESS.COM. You may also be interested in:
|
<<: Detailed explanation of Linx awk introductory tutorial
>>: Solve the MySQL 5.7.9 version sql_mode=only_full_group_by problem
Table of contents getApp() Define variables at th...
This article shares the installation tutorial of ...
Use ES6 modular development and observer mode to ...
This article example shares the specific code for...
The key features of the InnoDB storage engine inc...
How to set the position of the block element in t...
Table of contents What is VUE Core plugins in Vue...
Solution Abandon the Linux virtual machine that c...
In MySQL, how do you view the permissions a user ...
virtualenv is a tool for creating isolated Python...
One sentence to introduce HOC What is a higher-or...
Table of contents Parsers and preprocessors Query...
First of all, we need to know what a state manage...
Chinese documentation: https://router.vuejs.org/z...
Table of contents Overview 1. useState 1.1 Three ...