簡體   English   中英

我無法運行我的功能。 console.log將不會記錄

[英]I can't get my function to run. The console.log won't log

我有一個腳本,但我不知道為什么它無法正確執行。 我已經聲明了所有變量,直到提示它正常為止,但是我看不到為什么它不記錄函數的結果。

 var leasePriceString = prompt("Input lease price per month"); var ecoScoreString = prompt("Input eco score"); var catalogValueString = prompt("Input catalog value"); var c02String = prompt("Input C02"); var leasePrice = parseInt(leasePriceString); var ecoScore = parseInt(ecoScoreString); var catalogValue = parseInt(catalogValueString); var c02 = parseInt(c02String); var brutoMonth = true; var VAA = true; function calculator(){ function brutoMonthCalc(){ if (ecoScore >= 74){ brutoMonth = ((leasePrice*12)/13.92)-75; console.log(brutoMonth); } else { brutoMonth = ((leasePrice*12)/13.92)-150; console.log(brutoMonth); } } function VAACalc(){ VAA = 6/7*catalogValue*(0.055+((c02-105)*0.001)); console.log(VAA); } brutoMonthCalc(); VAACalc(); console.log("price per month is =" + brutoMonth + VAA); }; calculator(); 

我忘了讓它在某個地方運行嗎?

我已經修改了您代碼的某些方面,並在下面提供了以下說明:

  var leasePriceString = prompt("Input lease price per month"); var ecoScoreString = prompt("Input eco score"); var catalogValueString = prompt("Input catalog value"); var c02String = prompt("Input C02"); var leasePrice = parseInt(leasePriceString); var ecoScore = parseInt(ecoScoreString); var catalogValue = parseInt(catalogValueString); var c02 = parseInt(c02String); // Declare global variables here var brutoMonth; var VAA; function calculator(){ function brutoMonthCalc() { if (ecoScore >= 74) { brutoMonth = ((leasePrice * 12) / 13.92) - 75; console.log(brutoMonth); }else{ brutoMonth = ((leasePrice * 12) / 13.92) - 150; console.log(brutoMonth); } } function VAACalc() { VAA = 6 / 7 * catalogValue * (0.055 + ((c02 - 105) * 0.001)); console.log(VAA); } // Call functions here brutoMonthCalc(); VAACalc(); } calculator(); console.log("price per month is =" + brutoMonth + VAA); 

在上面的代碼中,我主要更改了兩件事:

  1. 聲明全局變量 :您只想聲明一次變量,然后在要運行以更改其值的函數中分配它們。

  2. 調用函數:編寫函數后,需要稍后對其進行調用。 僅當您調用它時,函數中的表達式才會運行。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM