View Full Version : NEED URGENT HELP!!! > Javascript Methods

05-24-2006, 09:33 AM
Hi guys, Once again im stuck on this darn assesment for school and i am really in urgent help with this one piece of code, so far i have the main section of the code created however i'm getting stuck on the creation of functions and putting them to use.
Heres what i'm going to need to add to the code i have already got:
> A Reader method ("toString") which returns a string conatining the details of the current state of an item. These details should include teh name, quantity, price & value.
> A Writer method for the name
> A Writer method for the price. This shuld only change the price attribute if the new price is a positive (can't have a negative number).
> A Writer method for the quantity. This should only change the quantity attribute if the ew quantity is a positive (can't have a negative items).


function Item(prod,quan,pric){
this.prod = prod;
this.quan = parseInt(quan);
this.pric = parseFloat(pric);

this.valueInStock = calcValue();

function calcValue(){
this.value = this.quan * this.pric;
return this.value.toFixed(2);
}//End Of calcValue

}//End Of tem

var prod = prompt("Please Enter Product","");
var quan = prompt("Please Enter Quantity","");
var pric = prompt("Please Enter Price","");

var myItem = new Item(prod,quan,pric);

document.write("<strong>Product Added :</strong> " + myItem.prod + "<br />");
document.write("<strong>Quantity :</strong> " + myItem.quan + "<br />");
document.write("<strong>Price :</strong> $" + myItem.pric + "<p>");
document.write("<strong>Total Value :</strong> $" + myItem.valueInStock + "");


If anyone could help me with this As soon as possible it would be like freaking awesome because this is really got me stuck.
Thanks In Advance,:thumbsup:

05-24-2006, 10:17 AM
I would have used a new Object instead of a new Function

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<title>Untitled Document</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<meta http-equiv="Content-Style-Type" content="text/css">
<meta http-equiv="Content-Script-Type" content="text/javascript">
<script type="text/javascript">
var arr=['Product Added :','Quantity : ','Price : ','Total Value : ']
function addItem(){
var prod = prompt('Please Enter Product','');
var quan = prompt('Please Enter Quantity','');
quan = prompt('No negative numbers allowed. Please Enter Quantity','');
var pric = prompt('Please Enter Price','');
pric = prompt('No negative numbers allowed. Please Enter Price','');
var valueInStock=quan*pric;
var myItem={'prod':prod,'quan':quan,'pric':pric,'valueInStock':valueInStock}//this is the new object
var root=document.getElementById('mydiv');
var i=0;
for(arg in myItem){
var s=document.createElement('span');
confirm('Do you want to add another item?')?addItem():null;
onload = addItem;
<div id="mydiv"></div>

05-24-2006, 10:36 AM
thanks for that Kor, you have no idea how much you just helped me.lol
feel like doing my full asisgnment? would probably be a piece of cake for you.lol. jk

05-24-2006, 10:46 AM
If there are lines/methods you don't understand, please feel free to write them, post them, and I will explain