When calculating the fare using javascript, I would like to make sure that commuter and commuter passes are available in conjunction with the fare when it comes out.

Asked 1 years ago, Updated 1 years ago, 20 views

I would like to implement it in JavaScript so that when I select two stations, information such as fares is calculated and displayed.

The fare is linked to the distance between the two stations.
In the current program, for example, if you choose medium-sized birds and deep-sea birds, the distance between them is calculated as 3.7km, and if it is 3.7km from there, you can see how much the fare is.In this case, it is 190 yen.

However, I would like to make a payment by linking the amount of commuting period and commuting period in conjunction with the fare.

Incidentally, the distance and fare between stations A and B have already been set on Nose Electric Railway's website. If you don't choose, you won't be able to find the value for commuting and commuting.
However, it is systematically linked, and if you pay the distance and fare, you will be able to commute to and from school.

Nose Electric Railway's HP
https://noseden.hankyu.co.jp/railway/

The following is a program where you have to choose two stations to get a fare.This is a program in which if you select two stations, you calculate this distance and pay the fare that matches that distance.

<!DOCTYPE html>
<metacharset="utf-8">
<title> </title>
<style>
</style>

<body>
<pid=A>
<select id=B>
    <option value="">--
        <option value="sb01">Middle-tongued bird
    <option value="sb02">Deep well
    <option value="sb03">Izumioka
        <option value="sb04">Togan/Mikita
    <option value="sb05"> Gwangmyeong Pond
    <option value="a0">a0
    <option value="a1">a1
    <option value="a2">a2
</select>

<select id=C>
    <option value="">--
        & lt ; options   value = gt Nakamozu and " b0 1 "
    & lt ; options   value = gt Fukai and " (see mount_nwfs (102 "
    & lt ; options   value = gt Izumi (ee) hill and " (see mount_nwfs (8 103 "
        & lt ; options   value = " (see mount_nwfs (104 " a polyboride & gt ; southern Japanese hemlock miki
    & lt ; options   value = gt Komyo-ike and " (see mount_nwfs (105 "
    <option value="b0">b0
    <option value="b1">b1
    <option value="b2">b2
</select>
</p>
<p id=D></p>

<script>

const vals = {
    'sb01sb02': 3.7,
    'sb01sb03': 7.8,
    'sb01sb04': 10.2,
    'sb01sb05': 12.1,
    'sb02sb03': 4.1,
    'sb02sb04': 6.5,
    'sb02sb05': 8.4,
    'sb03sb04': 2.4,
    'sb03sb05': 4.3,
    'sb04sb05': 1.9,
    'a0b0': 1.2,
    'a0b1'—12,
    'a0b2'—28.8,
  },
  len = [1, 2, 4, 6, 8, 10, 12, 14, 16],
  prc=['Freight 170(90), coupon 1,700(900), commuting_1,540(3,270)_3 months 18,640(9,320)_6 months 35,320(17,660), commuting_1,030(1,520)_3 months 8,640(4,320)_6 months 16,370(8,190)',
'運賃170(90),回数券1,700(900),通勤_1か月6,540(3,270)_3か月18,640(9,320)_6か月35,320(17,660),通学_1か月3,030(1,520)_3か月8,640(4,320)_6か月_16,370(8,190)',
'運賃190(100),回数券1,900(1,000),通勤_1か月7,360(3,680)_3か月20,980(10,490)_6か月39,750(19,880),通学_1か月3,470(1,700)_3か月9,690(4,850)_6か月_18,360(9,180)',
'運賃210(110),回数券2,100(1,100),通勤_1か月8,180(4,090)_3か月23,320(11,680)_6か月44,180(22,090),通学_1か月3,780(1,890)_3か月10,780(5,390)_6か月_20,420(10,210)',
'運賃230(120),回数券2,300(1,200),通勤_1か月8,990(4,500)_3か月25,630(12,820)_6か月48,550(24,280),通学_1か月4,160(2,080)_3か月11,860(5,930)_6か月_22,470(11,240)',
'運賃250(130),回数券2,500(1,300),通勤_1か月9,810(4,910)_3か月27,960(13,980)_6か月52,980(26,490),通学_1か月4,530(2,270)_3か月12,920(6,460)_6か月_24,470(12,240)',
'運賃270(140),回数券2,700(1,400),通勤_1か月10,620(5,310)_3か月30,270(15,140)_6か月57,350(28,680),通学_1か月4,910(2,460)_3か月14,000(7,000)_6か月_26,520(13,260)',
'運賃290(150),回数券2,900(1,500),通勤_1か月11,450(5,730)_3か月32,840(16,320)_6か月52,980(30,920),通学_1か月5,290(2,650)_3か月15,080(7,540)_6か月_28,570(14,290)'],
  f=(_,l=Math.floor(_))=>D.textContent=l?prc[len.findIndex(_=>_>l)]+'円':'';
A.addEventListener('change',_=>f(vals[(_=B.value)+(__=C.value)]||vals[__+_]));

</script>

What I did was write the prc and f= parts twice, and write the prc twice, f=(,l=Math.floor())=>D.textContent=l?prc[len.findIndex(=>l)]+f=(,l=Math.floor())=>D.textCent=[pent=l] +] Also, I will write prc twice, but I wrote the version without f=.
I'd like to display the fare and the regular fare separately.
Please tell me without omitting the js part.
If you write only the syntax, you may need to know where to put it, or you may need additional syntax.
I don't have the imagination to do that.

javascript

2022-09-30 18:16

1 Answers

Great advice

Let's stop using only one-dimensional structures and try to make them into two-dimensional arrays or put objects in objects.Finding the structure that connects data to data and storing it along that structure makes it easier to process.

For example, to store the distance between two stations, it is useful to use a two-dimensional object instead of a one-dimensional object.Imagine the spread of squares like a paper fare chart and the fare is written on each square.

const distances={
  'sb01': {
    'sb02': 3.7,
    'sb03': 7.8,
    'sb04': 10.2,
    'sb05': 12.1
  },
  'sb02': {
    'sb03': 4.1,
    'sb04': 6.5,
    'sb05': 8.4
  },
  // I'll omit it
};

If you write like this, the distance from sb01 to sb02 can be distances['sb01']['sb02'].

It is also recommended that you save only each data as an object and create a string based on the data.

constructions={
  1: {
    'ticket': {
      'adult': 170,
      'child': 90
    },
    'bookOfTicket': {
      'adult': 1700,
      'child': 900
    },
    'communicationTicket': {
      'oneMonth': {
        'adult': 6540,
        'child'—3270
      },
      'threeMonths': {
        'adult': 18640,
        'child': 9320,
      },
      'sixMonths': {
        'adult': 35320,
        'child'—17660
      }
    },
    'studentTicket': {
      'oneMonth': {
        'adult' : 3030,
        'child'—1520
      },
      'threeMonths': {
        'adult': 8640,
        'child':4320,
      },
      'sixMonths': {
        'adult'—16370,
        'child'—8190
      }
    }
  },
  // It will be longer, so I will omit the following.
}

Also, if it is possible to calculate various fares from distance by mathematical formula, it is better to make a function and calculate it every time instead of saving it as data like this.


2022-09-30 18:16

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.