2013-06-24 127 views
-1

我對javascript有太多的麻煩。我應該使用函數來製作一個簡單的薪水檢查程序。我不知道我在做什麼錯。我也必須將payratetaxrate更改爲十進制格式。你如何把它放在代碼中?這是我的代碼的樣子。Javascript函數問題

<html> 
    <body bgcolor="#81DAF5"> 
    <head> 
    <title>Chapter 4 Assignment 1</title> 
    </head> 
    <body> 
    <script type="text/javascript"> 
     var hoursworked = window.prompt("How many hours worked?", ""); 
     var payrate = window.prompt("What is the pay rate?", ""); 
     var taxrate = window.prompt("What is the tax rate?", ""); 
     var netpay 
     parseFloat(payrate).toFixed(2); 
     parseFloat(taxrate).toFixed(2); 
     function calculatepay() 
     { 
     var grosspay= hoursworked * payrate; 
     var taxamount= (grosspay * taxrate)/100; 
     var netpay= grosspay - taxamount; 
     return netpay; 
     } 

     document.write("<h1><b>Hours Worked: " +hoursworked+ "<br></h1></b>"); 
     document.write("<h1><b>Hourly payrate: " +payrate+ "<br></h1></b>"); 
     document.write("<h1><b>Tax rate applied: " +taxrate+ "<br></h1></b>"); 
     document.write("<h1><b>Net Pay = " +calculatepay+ "<br></h1></b>"); 
    </script> 
    </body> 
</html> 
+2

'hoursworked','payrate'和'taxrate'在哪裏定義?你在做什麼回報價值? –

+0

你有什麼問題? – SLaks

+0

該程序開始向用戶詢問提示。說「工作了多少小時,你的工資率是多少,稅率是多少?」 – jaramore

回答

0

感謝您的更新。雖然parseFloat(payrate).toFixed(2)確實似乎將存儲的值轉換爲浮點數,但它將存儲答案as a string

此外,您應該將結果分配給另一個變量,或者在任何情況下返回payrate,否則float/string將會丟失。

嘗試:

payrate = parseFloat(payrate).toFixed(2); 
taxrate = parseFloat(taxrate).toFixed(2); 

,然後在calculatepay()功能:

// neat trick at http://stackoverflow.com/a/5661399/1091386 
// allows you to use a string as a float in addition/subtraction 
// apparently not necessary with multiplication but I thought I'd leave it in 

var taxamount = hoursworked * +(payrate); 

最後,不要忘記在與()末調用calculatepay()或者你只是返回功能。

0

當您使用 parseFloat(payrate).toFixedString(2); 實際值不變。

+0

將它們更改爲十進制格式的代碼是什麼?我如何修復這個函數,所以netpay在輸出時不會被定義。 – jaramore

+0

只需將它們恢復到自己的變量,如: Payrate = parseFloat(payrate).toFixedString(2) –

1

有在代碼中幾個問題

  1. 解析輸入字符串後,您需要返回的值分配回變量前payrate = parseFloat(payrate).toFixed(2);
  2. 你需要通過加入()調用該函數calculatepay最終打印結果

嘗試

var hoursworked = window.prompt("How many hours worked?", ""); 
    var payrate = window.prompt("What is the pay rate?", ""); 
    var taxrate = window.prompt("What is the tax rate?", ""); 
    payrate = parseFloat(payrate).toFixed(2); 
    taxrate = parseFloat(taxrate).toFixed(2); 
    function calculatepay() 
    { 
    var grosspay= hoursworked * payrate; 
    var taxamount= (grosspay * taxrate)/100; 
    var netpay= grosspay - taxamount; 
    return netpay; 
    } 

    var netpay = calculatepay(); 

    document.write("<h1><b>Hours Worked: " +hoursworked+ "<br></h1></b>"); 
    document.write("<h1><b>Hourly payrate: " +payrate+ "<br></h1></b>"); 
    document.write("<h1><b>Tax rate applied: " +taxrate+ "<br></h1></b>"); 
    document.write("<h1><b>Net Pay = " +netpay+ "<br></h1></b>"); 

演示:Plunker

+0

哦!謝謝!我現在看到它。最後我用netpay代替calculatepay()。 – jaramore

+0

@jaramore查看更新,在這種情況下,您需要將計算值分配給變量'netpay' –

0

我已經爲您更新了腳本。一些小錯誤。在調用計算支付函數時,您沒有包含支架,而且您也沒有存儲應用parseFloat方法的結果。

應用括號給出:

document.write("<p>Net Pay = " + calculatepay() + "</p>"); 

存儲值:

payrate = parseFloat(payrate).toFixed(2); 
taxrate = parseFloat(taxrate).toFixed(2); 

還你不需要單獨聲明每個變量,你可以通過簡單地將它們與分離做所有這些作爲一個組逗號:

var hoursworked = window.prompt("How many hours worked?", ""), 
    payrate = window.prompt("What is the pay rate?", ""), 
    payrate = parseFloat(payrate).toFixed(2), 
    taxrate = window.prompt("What is the tax rate?", ""), 
    taxrate = parseFloat(taxrate).toFixed(2), 
    netpay = 0; 

這裏是一個工作示例 - http://jsfiddle.net/2UVyH/2

希望可以幫到