korkie
korkie

Reputation: 29

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.html:15 Uncaught ReferenceError: CheckSelection is not defined

HTML code:

<?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:

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.

Upvotes: -1

Views: 145

Answers (1)

Burrito
Burrito

Reputation: 515

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 '

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. 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

Upvotes: 3

Related Questions