views:

171

answers:

2

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"&gt;
<html xmlns="http://www.w3.org/1999/xhtml"&gt;

<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>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</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>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</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>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</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>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</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>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</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>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</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>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</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>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</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>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</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>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</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>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</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!

A: 

Maybe just run it, use it, and see what goes wrong. The greater challenge will be how to fill the data dynamically, from some database, since your stock of supplies will change regularly.

As for posting the data to the server, be sure that on the server you re-check everything, i.e. don't rely on anything your javascript does - it can easily be tweaked by a malicious user.

mvds
I just have a question , if a code does the same thing just like another code did , and the code is shorter , is it the shorter one is the more efficient code??
not necessarily. You can take a car, drive 100 miles and arrive earlier at your destination compared to walking 50 miles in a straight line - what would be more efficient? in other words: it totally depends on your goals.
mvds
+1  A: 

The code would be more flexible, if you use a pointer to the clicked element itself, instead of giving the index of the element to the update-functions. You can achieve this by changing the onclick-attribute from onclick="Addlist_onclick(0)" (or any other index) to onclick="Addlist_onclick(this)" and changing the Addlist_onclick code a bit:

function Addlist_onclick(node)
{
    var comp = node.form;
    var count;
    var position;

    switch(node.name)
    {
        case "process": position = 0;
                        break;
        case "Hard":        position = 1;
                        break;
        default:            position = 2;
                        break;
    } 

    comp.list.options[position].text = node.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;
}

You can improve AddCmp_onclick in a similar way, but I leave this as an exercise to you ;)

How it works in detail: In Javascript you can use the this-pointer if you need a reference to the current context (the object the method is part of). If you you simply declare a function inside a script-tag (like you did with Addlist_onclick), the declared function becomes a method of the window object. Therefore this would point to window.

However, if you add an event handler to a html-node through an attribute like onclick, the javascript-interpreter first calls a generic event-handling method of the clicked html-node, which then calls the function you've defined in the attribute. Therefore the this points to the clicked html-node when Addlist_onclick(this) is executed. Inside Addlist_onclick the this-pointer points to window again, since your function is not a method of the clicked-node but of the main-window (as explained above).

Le Torbi