亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

在 React 中計算總價

在 React 中計算總價

Cats萌萌 2023-05-11 10:11:25
所以這是我第一次嘗試使用 React。我必須計算預訂的(總)價格。保留價格由以下幾個因素決定:船長:每米 1.25 歐元人數:每人1歐元電費:每天 1.25 歐元假設船長 10 米,船上有 2 個人用電。他們待了 2 天。計算如下:船長 (10) * 1.25 * 2 = 25人數(2)* 1 * 2 = 4使用電力(真)= 1.25 * 2 = 2.5總價 = 25 歐元 + 4 歐元 + 2.5 歐元 = 21.5 歐元我嘗試在我的代碼中實現它,它看起來像這樣:import React, { Component } from 'react'import BoatForm from "./BoatForm";import StayForm from "./StayForm";export default class PriceCalculator extends Component {    /**     * So, we created a form for the customers so that they can register their boat.     * Now we want to let them know how much their reservation costs.     * The size of the boat, amount of people, amount of days and use of electricity are all parameters.     */    constructor(props) {        super(props)        this.state = {            boat_length: '',            amount_of_people: '',            arrival_date: '',            leave_date: '',            use_of_electricity: '',            box_number: ''        }    }    /**     * We use date fields in our form to define the stay date of the customer.     * That means we have to calculate the days ourselves.     */    calculateStayTime() {        const ArrivalDate = new Date(this.state.arrival_date);        const LeaveDate = new Date(this.state.leave_date);        // calculate difference        const DiffTime = Math.abs(LeaveDate - ArrivalDate);        const DiffDays = Math.ceil(DiffTime / (1000 * 60 * 60 * 24));        let AmountOfDays = DiffDays;        return AmountOfDays;       }}我努力將變量從 calculateStayTime() 傳遞到 calculatePrice()。任何人都可以幫助我如何將我的計算公式化為 React.JS?
查看完整描述

1 回答

?
陪伴而非守候

TA貢獻1757條經驗 獲得超8個贊

calculateStayTime您需要使用inside的結果calculatePrice。您可以將停留時間值分配給變量,但我可能只是替換最后一行:// const total_price = price_per_day * AmountOfDays;可以替換為const total_price = price_per_day * this.calculateStayTime();which 應該可以滿足您的需求。



查看完整回答
反對 回復 2023-05-11
  • 1 回答
  • 0 關注
  • 192 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號