Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to calculate the starting station fare by using the object writing method of JavaScript array

2025-01-21 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >

Share

Shulou(Shulou.com)06/01 Report--

This article will explain in detail how to use JavaScript array object writing method to calculate the starting station fare. The content of the article is of high quality, so the editor will share it for you as a reference. I hope you will have a certain understanding of the relevant knowledge after reading this article.

Calculate the fare at the starting station

Topic: Shenzhen-60 murals-Guangzhou-50-Humen-40-Zhongshan-36-Zhuhai-34-Macau-891 Hong Kong above fare calculation, such as 60 yuan by car from Shenzhen to Guangzhou, 50 yuan from Guangzhou to Humen, 60 yuan from Shenzhen to Humen, and so on.

Please use a function to pass in a starting point and an end point to calculate the fare. If the fare is passed into Humen, the starting point, Macau will calculate 110 yuan.

Thinking

First of all, let's think about how to connect each station with the fare.

Very simply, we use an object to deposit {city,price}, the ticket price from each station and each station to the next station, and 0 at the last station, which shows a certain rule.

Encapsulate a function, pass an array object, an object at the beginning and end station

In the function, we first check whether the starting station and the terminal are reasonable.

Use indexOf to get the subscript and see which direction it is going.

Use for to loop through the data object, and then you can calculate the fare

The running result is as follows

The following is the ticket billing problem that I wrote with js code.

Objects stored in Document / / array var array = [{city:' Shenzhen', price:0}, {city:' Guangzhou', pric. On how to use the JavaScript array object writing method to calculate the starting station fare is shared here, I hope the above content can be of some help to you, can learn more knowledge. If you think the article is good, you can share it for more people to see.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Servers

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report