JS realizes the calculation of the total price of goods in the shopping cart

JS realizes the calculation of the total price of goods in the shopping cart

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:
  • js method to realize shopping cart calculation

<<:  Detailed explanation of Linx awk introductory tutorial

>>:  Solve the MySQL 5.7.9 version sql_mode=only_full_group_by problem

Recommend

How webpack implements static resource caching

Table of contents introduction Distinguish betwee...

The difference between where and on in MySQL and when to use them

When I was writing join table queries before, I a...

Vue.js implements the code of clicking the icon to zoom in and leaving

The previous article introduced how Vue can reali...

Essential conditional query statements for MySQL database

Table of contents 1. Basic grammar 2. Filter by c...

Summary of B-tree index knowledge points in MySQL optimization

Why do we need to optimize SQL? Obviously, when w...

JavaScript Shorthand Tips

Table of contents 1. Merge arrays 2. Merge arrays...

Detailed steps to use Arthas in a Docker container

What can Arthas do for you? Arthas is Alibaba'...

Modify the maximum number of mysql connections and configuration files in docker

1. Find the mysql image docker ps 2. Enter the mi...

How to quickly copy large files under Linux

Copy data When copying data remotely, we usually ...

Tutorial on building nextcloud personal network disk with Docker

Table of contents 1. Introduction 2. Deployment E...

Summary of 10 must-see JavaScript interview questions (recommended)

1.This points to 1. Who calls whom? example: func...

11 Reasons Why Bootstrap Is So Popular

Preface Bootstrap, the most popular front-end dev...

19 MySQL optimization methods in database management

After MySQL database optimization, not only can t...

Detailed steps to install RabbitMQ in docker

Table of contents 1. Find the mirror 2. Download ...

iframe multi-layer nesting, unlimited nesting, highly adaptive solution

There are three pages A, B, and C. Page A contains...