简体   繁体   English

我的代码有什么问题?

[英]What is wrong with my code?

I'm having trouble with my code but can't seem to find what the problem is. 我的代码遇到问题,但似乎找不到问题所在。 I keep getting the same errors in browser but I don't see what the problem is? 我在浏览器中不断遇到相同的错误,但看不到问题出在哪里?

FVOM.js:16 Uncaught SyntaxError: Unexpected number FVOM.js:16未捕获的语法错误:意外的数字

FVOM.html:15 Uncaught ReferenceError: CheckSelection is not defined FVOM.html:15未捕获的ReferenceError:未定义CheckSelection

HTML code: HTML代码:

<?xml version="1.0" encoding="ISO-8859-1" ?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
    <title>Future Value Demo</title>
    <link rel="stylesheet" type="text/css" href="css/index.css"/>
    <script type="text/javascript" src="JScripts/FVOM.js"></script>
</head>
<body>
    <table>
        <tr>
            <td colspan="2">
                <input type="radio" id="futVal" name="Money" checked onClick="FVOM:CheckSelection(1)"/>Future Value
                <input type="radio" id="preVal" name="Money" onClick="FVOM:CheckSelection(2)"/>Present Value
                <input type="radio" id="rate" name="Money" onClick="FVOM:CheckSelection(3)"/>Rate of Interest
                <input type="radio" id="time" name="Money" onClick="FVOM:CheckSelection(4)"/>Years
            </td>
        </tr>
    </table>
    <br/>
    <table>
        <tr>
            <td>Future Value (FV)</td>
            <td><input type="text" id="FV" disabled/></td>
        </tr>
        <tr>
            <td>Present Value (PV)</td>
            <td><input type="text" id="PV" value = "1500" /></td>
        </tr>
        <tr>
            <td>Interest Rate [pa] (r)</td>
            <td><input type="text" id="R" value = "4.5"/></td>
        </tr>
        <tr>
            <td>Years (n)</td>
            <td><input type="text" id="N" value = "1"/></td>
        </tr>
        <tr>
            <td>Compounded</td>
            <td>
                <select id="compounded">
                     <option value="year">Yearly</option>
                     <option value="quarter">Quarterly</option>
                     <option value="month">Monthly</option>
                </select>
            </td>
        </tr>
        <tr>
            <td><input type="button" id="reset" value="RESET"/></td>
            <td><input type="button" id="calculate" value="Calculate"/></td>
        </tr>
    </table>
    <br/>
    <hr/>
    <br/>
    <div id="results">

    </div>
</body>

JavaScript: JavaScript:

var $ = function(id){
return document.getElementById(id);
};
var futureValue = function(){
var pv = parseFloat($("PV").value);
var r = parseFloat($("R").value);
var n = parseFloat($("N").value);

if(isNaN(pv) || isNaN(r) || isNaN(n)){
    alert("Please enter a valid number");
}
else{
    r = r/100/Compounded();
    n = n*Compounded();
    $("FV").value=(pv*Math.pow((1+r), n)).toFixed(2);

    var res = "<table border="1"><tr><th>Period</th><th>Investment</th><th>Year End</th></tr>";
    for(var i=1; i<=n; i++){
        res+="<tr><td>"+i+"</td><td>&euro;"+(pv).toFixed(2)+"</td><td>&euro;"+(pv*Math.pow((1+r), 1)).toFixed(2)+"</td></tr>";
        pv = pv*Math.pow((1+r), 1);
    }
    res+="</table>";
    $("results").innerHTML = res;
}
};
var presentValue = function(){
var fv = parseFloat($("FV").value);
var r = parseFloat($("R").value);
var n = parseFloat($("N").value);

if(isNaN(fv) || isNaN(r) || isNaN(n))
    alert("Please enter numbers");
else{
    r = r/100/Compounded();
    n = n*Compounded();
    $("PV").value=(fv/Math.pow((1+r), n)).toFixed(2);
}
};
var rateOfInterest = function(){
var fv = parseFloat($("FV").value);
var pv = parseFloat($("PV").value);
var n = parseFloat($("N").value);

if(isNaN(fv) || isNaN(pv) || isNaN(n))
    alert("Please enter numbers");
else{
    n = n*Compounded();
    $("R").value=((Math.pow((fv/pv),(1/n))-1)*100*Compounded()).toFixed(2)+"%";
}
};
var numberOfYears = function(){
var fv = parseFloat($("FV").value);
var pv = parseFloat($("PV").value);
var r = parseFloat($("R").value);

if(isNaN(fv) || isNaN(pv) || isNaN(r))
    alert("Please enter numbers");
else{
    r = r/100/Compounded;
    $("N").value=(((Math.log(fv)-Math.log(pv))/Math.log(1 + r)))n/Compounded()).toFixed(2);
}
};
var Compounded = function(){
var com = $("compounded").value;
if(com=="year")
    return 1;
if(com=="quarter")
    return 4;
if(com=="month")
    return 12;
};
var calculate = function(){
if($("futVal").checked)
    futureValue();
if($("preVal").checked)
    presentValue();
if($("rate").checked)
    rateOfInterest();
if($("time").checked()
    numberOfYears();
};
var CheckSelection = function(id){
if(id==1){
    $("FV").disabled = true;
    $("PV").disabled = false;
    $("R").disabled = false;
    $("N").disabled = false;
}
else if(id==2){
    $("FV").disabled = false;
    $("PV").disabled = true;
    $("R").disabled = false;
    $("N").disabled = false;
}
else if(id==3){
    $("FV").disabled = false;
    $("PV").disabled = false;
    $("R").disabled = true;
    $("N").disabled = false;
}
else{
    $("FV").disabled = false;
    $("PV").disabled = false;
    $("R").disabled = false;
    $("N").disabled = true;
}
RESET();
};  
var RESET = function(){
$("FV").value = "";
$("PV").value = "";
$("R").value = "";
$("N").value = "";
    $("results").innerHTML = "";
};
window.onload = function(){
$("calculate").onClick = calculate;
$("reset").onClick = RESET;
};

I'm fairly new to JavaScript so any help would be greatly appreciated. 我对JavaScript相当陌生,因此不胜感激。

var res = "<table border="1"><tr><th>Period</th><th>Investment</th><th>Year End</th></tr>";

is where the problem lies. 是问题所在。 You need to escape the " (double quotes) that exist in border="1" by either using a \\" or by using single quotes ' 您需要使用\\“或使用单引号'来使border =” 1“中的”(双引号)转义。

For this reason some people use single quotes in javascript to assign a string, that way you do not need to escape the double quotes. 因此,有些人在javascript中使用单引号来分配字符串,因此您无需转义双引号。 If you would like to read more about strings in javascript there is a decent introduction at http://www.w3schools.com/js/js_strings.asp 如果您想了解有关JavaScript中字符串的更多信息, 请访问http://www.w3schools.com/js/js_strings.asp,以获取不错的介绍

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM