This might be the last question I post on exercises I'm doing in this Javascript book I've purchased -- it really isn't giving enough background for novices to do the exercises based on the examples and explanations. So the question is:
"modify the javascript code so it provides for the option buttons. When the user clicks the Yearly interest button [this is an interest calculator using JS] the application should compound the interest yearly. When the user clicks the Monthly interest button, the application should compound the interest monthly".
Yuck. You will see how I've started the code (see comment lines), but I just don't have a clue on how to continue. Perhaps based on the answers I receive, I'll make a decision on whether or not I need to find a more remedial source for instruction. I have to admit, I liked the book because it shows real web applications using javascript. I just can't take any more tutorials with 'document.write "hello world!" ' anymore. But if I don't understand the basics of the exercises in the book, I might need to step back a bit in my js self-training. Thanks for letting me rant. So, here's the code:
THE HTML CODE
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"<html xmlns="<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Future Value Calculator</title>
<style type="text/css">
@import url("future_value.css");
</style>
<script type="text/javascript" src="future_value.js"></script>
</head>
<body>
<div id="content">
<h1>Future Value Calculator</h1>
<p><input type="radio" name="calctype" id="monthly"
value="monthly" checked />Monthly Interest</p>
<p><input type="radio" name="calctype" id="yearly"
value="yearly" />Yearly Interest</p>
<p>Enter the values below and click "Calculate".</p>
<label for="investment">One-Time Investment:</label>
<select name="investment" id="investment"
<optgroup label="">
<option value="1000">$1,000</option>
<option value="5000">$5,000</option>
<option value="10000">$10,000</option>
<option value="25000">$25,000</option>
<option value="other">Other value</option>
</optgroup>
</select><br />
<label for="rate">Yearly Interest Rate:</label>
<select name="rate" id="rate"
<optgroup label="">
<option value="4%">4%</option>
<option value="6%">6%</option>
<option value="8%">8%</option>
<option value="10%">10%</option>
</optgroup>
</select><br />
<label for="years">Number of Years:</label>
<input type="text" id="years" /><br />
<label for="futureValue">Future Value:</label>
<input type="text" id="futureValue" disabled="disabled" /><br />
<label> </label>
<input type="button" id="calculate" value="Calculate" /><br />
<p><input type="checkbox" name="display" id="display" value="display" checked />
Display both monthly and yearly results in the text area.</p>
<p>Results</p>
<textarea name="results" id="results" rows="4" cols="50"></textarea>
</div>
</body>
</html>
THE JAVASCRIPT CODE
var $ = function (id) {
return document.getElementById(id);
}
var calculate_click = function () {
var investment = parseFloat( $("investment").value );
var annualRate = parseFloat( $("rate").value );
var years = parseInt( $("years").value );
var monthly = parseInt ( $("monthly").value); //I think I need to add this variable?
var yearly = parseInt ( $("yearly").value); // I think I need to add this variable?
$("futureValue").value = "";
if (isNaN(investment) || investment <= 0) {
alert("Investment must be a valid number\nand greater than zero.");
} else if(isNaN(annualRate) || annualRate <= 0) {
alert("Annual rate must be a valid number\nand greater than zero.");
} else if(isNaN(years) || years <= 0) {
alert("Years must be a valid number\nand greater than zero.");
} else {
var monthlyRate = annualRate / 12 / 100;
var months = years * 12;
var futureValue = investment;
for ( i = 1; i <= months; i++ ) {
futureValue = futureValue * (1 + monthlyRate);
}
$("futureValue").value = futureValue.toFixed(2);
}
}
//this is as far as I got with the code to enable the radio buttons. See below.
// FYI--calculate button does not work with this code inserted.
// FF error console and Firebug flags the first line of the "if" statement containing the "monthly" ID as "null".
var status = "unknown";
if ( $("monthly").checked ) {
status = $("monthly").value;
}
if ( $("yearly").checked ) {
status =$("yearly").value;
}
window.onload = function () {
$("calculate").onclick = calculate_click;
$("investment").focus();
}
"modify the javascript code so it provides for the option buttons. When the user clicks the Yearly interest button [this is an interest calculator using JS] the application should compound the interest yearly. When the user clicks the Monthly interest button, the application should compound the interest monthly".
Yuck. You will see how I've started the code (see comment lines), but I just don't have a clue on how to continue. Perhaps based on the answers I receive, I'll make a decision on whether or not I need to find a more remedial source for instruction. I have to admit, I liked the book because it shows real web applications using javascript. I just can't take any more tutorials with 'document.write "hello world!" ' anymore. But if I don't understand the basics of the exercises in the book, I might need to step back a bit in my js self-training. Thanks for letting me rant. So, here's the code:
THE HTML CODE
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"<html xmlns="<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Future Value Calculator</title>
<style type="text/css">
@import url("future_value.css");
</style>
<script type="text/javascript" src="future_value.js"></script>
</head>
<body>
<div id="content">
<h1>Future Value Calculator</h1>
<p><input type="radio" name="calctype" id="monthly"
value="monthly" checked />Monthly Interest</p>
<p><input type="radio" name="calctype" id="yearly"
value="yearly" />Yearly Interest</p>
<p>Enter the values below and click "Calculate".</p>
<label for="investment">One-Time Investment:</label>
<select name="investment" id="investment"
<optgroup label="">
<option value="1000">$1,000</option>
<option value="5000">$5,000</option>
<option value="10000">$10,000</option>
<option value="25000">$25,000</option>
<option value="other">Other value</option>
</optgroup>
</select><br />
<label for="rate">Yearly Interest Rate:</label>
<select name="rate" id="rate"
<optgroup label="">
<option value="4%">4%</option>
<option value="6%">6%</option>
<option value="8%">8%</option>
<option value="10%">10%</option>
</optgroup>
</select><br />
<label for="years">Number of Years:</label>
<input type="text" id="years" /><br />
<label for="futureValue">Future Value:</label>
<input type="text" id="futureValue" disabled="disabled" /><br />
<label> </label>
<input type="button" id="calculate" value="Calculate" /><br />
<p><input type="checkbox" name="display" id="display" value="display" checked />
Display both monthly and yearly results in the text area.</p>
<p>Results</p>
<textarea name="results" id="results" rows="4" cols="50"></textarea>
</div>
</body>
</html>
THE JAVASCRIPT CODE
var $ = function (id) {
return document.getElementById(id);
}
var calculate_click = function () {
var investment = parseFloat( $("investment").value );
var annualRate = parseFloat( $("rate").value );
var years = parseInt( $("years").value );
var monthly = parseInt ( $("monthly").value); //I think I need to add this variable?
var yearly = parseInt ( $("yearly").value); // I think I need to add this variable?
$("futureValue").value = "";
if (isNaN(investment) || investment <= 0) {
alert("Investment must be a valid number\nand greater than zero.");
} else if(isNaN(annualRate) || annualRate <= 0) {
alert("Annual rate must be a valid number\nand greater than zero.");
} else if(isNaN(years) || years <= 0) {
alert("Years must be a valid number\nand greater than zero.");
} else {
var monthlyRate = annualRate / 12 / 100;
var months = years * 12;
var futureValue = investment;
for ( i = 1; i <= months; i++ ) {
futureValue = futureValue * (1 + monthlyRate);
}
$("futureValue").value = futureValue.toFixed(2);
}
}
//this is as far as I got with the code to enable the radio buttons. See below.
// FYI--calculate button does not work with this code inserted.
// FF error console and Firebug flags the first line of the "if" statement containing the "monthly" ID as "null".
var status = "unknown";
if ( $("monthly").checked ) {
status = $("monthly").value;
}
if ( $("yearly").checked ) {
status =$("yearly").value;
}
window.onload = function () {
$("calculate").onclick = calculate_click;
$("investment").focus();
}