I'm a self learner in javascript,currently I'm still a beginner so I just need some advice from some expert about the exercise given by a javascript book,without referring to the answer because I have the urge to learn,i coded out a simple site request by the question.
The question is as follow:
Create a user interface that allows the user to pick the computer system of their dreams, similar in principle to the e-commerce sites selling computers over the Internet. For example, they could be given a choice of processor type, speed, memory, and hard drive size, and the option to add additional components like a DVD-ROM drive, a sound card, and so on. As the user changes their selections, the price of the system should update automatically and notify them of the cost of the system as they specifi ed it, either by using an alert box or by updating the contents of a text box.
The solution by me
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<script type="text/javascript">
function Addlist_onclick(index)
{
var comp = document.compSpec;
var count;
var position;
if( index <= 11 )
{
position = 0;
}
else if( index > 11 && index < 18)
{
position = 1;
}
else
{
position = 2;
}
comp.list.options[position].text = comp.elements[index].value;
count = comp.list.options[position].text.lastIndexOf("RM");
comp.list.options[position].value = comp.list.options[position].text.substr(count + 2);
comp.totalPrice.value = null;
}
function AddCmp_onclick(index)
{
var comp = document.compSpec;
if(comp.Add[index].checked == true)
{
var price = comp.Add[index].value.lastIndexOf("RM");
var newOption = new Option(comp.Add[index].value , comp.Add[index].value.substr(price + 2));
try
{
comp.list.add(newOption , comp.list.options[comp.list.length]);
}
catch(error)
{
comp.list.add(newOption , comp.list.length);
comp.totalPrice.value = null;
}
}
else
{
for(var i = comp.list.length - 1 ; i > 2 ; i--)
{
if(comp.list.options[i].text == comp.Add[index].value)
{
comp.list.remove(i);
break;
}
}
}
}
function priceCalc_onclick()
{
var comp = document.compSpec;
var total = 0;
for(var i = 0 ; i < comp.list.length ; i++)
{
total += Number(comp.list.options[i].value);
}
comp.totalPrice.value = "RM " + total.toFixed(2);
}
</script>
</head>
<body>
<table>
<tr>
<td><h2>Welcome to Junior Computer Centre</h2></td>
</tr>
</table>
<form name="compSpec">
<h3>Select the processor model and speed you prefer:</h3>
<table cellspacing="" align="center">
<tr>
<th><h4>Intel</h4></th>
<td> </td>
<th><h4>AMD</h4></th>
</tr>
<tr>
<td><input type="radio" name="process" value="Intel Processor 1.3 Ghz - RM140" checked="checked" onclick="Addlist_onclick(0)" /> 1.3 Ghz - RM 140</td>
<td> </td>
<td><input type="radio" name="process" value="AMD Processor 1.3 Ghz - RM140" onclick="Addlist_onclick(1)" /> 1.3 Ghz - RM 140</td>
</tr>
<tr>
<td><input type="radio" name="process" value="Intel Processor 2.6 Ghz - RM180" onclick="Addlist_onclick(2)"/> 2.6 Ghz - RM 180</td>
<td> </td>
<td><input type="radio" name="process" value="AMD Processor 2.6 Ghz - RM180" onclick="Addlist_onclick(3)" /> 2.6 Ghz - RM 180</td>
</tr>
<td><input type="radio" name="process" value="Intel Processor 2.8 Ghz - RM229" onclick="Addlist_onclick(4)" /> 2.8 Ghz - RM 229</td>
<td> </td>
<td><input type="radio" name="process" value="AMD Processor 2.8Ghz - RM229" onclick="Addlist_onclick(5)" /> 2.8 Ghz - RM229</td>
</tr>
<tr>
<td><input type="radio" name="process" value ="Intel Processor 3.2Ghz - RM250" onclick="Addlist_onclick(6)" /> 3.2 Ghz - RM 250</td>
<td> </td>
<td><input type="radio" name="process" value="AMD Processor 3.2Ghz - RM250" onclick="Addlist_onclick(7)" /> 3.2 Ghz - RM 250</td>
</tr>
<tr>
<td><input type="radio" name="process" value="Intel Processor 3.6Ghz - RM300" onclick="Addlist_onclick(8)" /> 3.6 Ghz - RM 300 </td>
<td> </td>
<td><input type="radio" name="process" value="AMD Processor 3.6Ghz - RM300" onclick="Addlist_onclick(9)" /> 3.6 Ghz - RM 300 </td>
</tr>
<tr>
<td><input type="radio" name="process" value="Intel Processor 3.8Ghz - RM320" onclick="Addlist_onclick(10)" /> 3.8 Ghz - RM 320</td>
<td> </td>
<td><input type="radio" name="process" value="AMD Processor 3.8Ghz - RM320" onclick="Addlist_onclick(11)" /> 3.8 Ghz - RM 320</td>
</tr>
</table>
<h3>Select the capacity of hard disk you prefer:</h3>
<table align="center">
<tr>
<th><h4>Buffalo</h4></th>
<td> </td>
<th><h4>Logitech</h4></th>
</tr>
<tr>
<td><input type="radio" name="Hard" checked="checked" value="Buffalo Harddisk 320GB - RM101" onclick="Addlist_onclick(12)" /> 320 GB - RM 101</td>
<td> </td>
<td><input type="radio" name="Hard" value="Logitech Harddisk 320GB - RM101" onclick="Addlist_onclick(13)" /> 320 GB - RM 101</td>
</tr>
<tr>
<td><input type="radio" name="Hard" value="Buffalo Harddisk 640GB - RM180" onclick="Addlist_onclick(14)" /> 640 GB - RM 180</td>
<td> </td>
<td><input type="radio" name="Hard" value="Logitech Harddisk 640GB - RM180" onclick="Addlist_onclick(15)" /> 640 GB - RM 180</td>
</tr>
<tr>
<td><input type="radio" name="Hard" value="Buffalo Harddisk 1020GB - RM240" onclick="Addlist_onclick(16)" /> 1020 GB - RM 240</td>
<td> </td>
<td><input type="radio" name="Hard" value="Logitech Harddisk 1020GB - RM240" onclick="Addlist_onclick(17)" /> 1020 GB - RM 240</td>
</tr>
</table>
</select>
<h3>Select your preferable RAM capacity:</h3>
<table align="center">
<tr>
<th><h4>Logitech</h4></th>
</tr>
<tr>
<td><input type="radio" name="RAM" checked="checked" value="Logitech RAM 2GB - RM250" onclick="Addlist_onclick(18)"> 2 GB - RM 250 </td>
</tr>
<tr>
<td><input type="radio" name="RAM" value="Logitech RAM 4GB - RM430" onclick="Addlist_onclick(19)"> 4 GB - RM 430 </td>
<tr>
</table>
<h3>Select any additional components(optional):</h3>
<table align="center">
<tr>
<td><input type="checkbox" name="Add" value="DVD-ROM - RM65" onclick="AddCmp_onclick(0)" > DVD-ROM - RM 65 </td>
<td><input type="checkbox" name="Add" value="Speaker - RM32" onclick="AddCmp_onclick(1)" > Speaker - RM 32</td>
<td><input type="checkbox" name="Add" value="Fan - RM10.50" onclick="AddCmp_onclick(2)" > Fan - RM10.50</td>
</tr>
</table>
<br />
<br />
<h2>Components Chosen</h2>
<select multiple="multiple" name="list">
<option value="140">Intel Processor 1.3 Ghz - RM140</option>
<option value="101">Buffalo Harddisk 320GB - RM101</option>
<option value="250">Logitech RAM 2GB - RM250</option>
</select>
<br />
<br />
<br />
Total amount : <input type="text" name="totalPrice" readonly="readonly" />
<br />
<input type="button" value="Calculate Price" onclick="priceCalc_onclick()" />
</form>
</body>
</html>
Is there any mistake or anything I need to addon or anything I can do to further enhance the code??The thing I concerned the most is my javascript function as I wanted to master myself in coding,so any suggestion and teaching will be much appreciated,Thank you!