...

View Full Version : HELP ASAP PLEASE - JS Objects (simplified?)



satrah
05-25-2006, 10:33 AM
Hi Guys,
Once again im stuck, thanks first of all to KOR, you have been a huge help however as a n00b im going to need some help working this script out, basically i need this script converted to a more simplified version using functions, there are two pieces of code which i have so far of which i will quickly explain now...

This is my original piece of code, it works so far but there are more features to be added to it wihich i will explain with the next pice of code:

<script>
var prod = prompt("Enter The Product","")
var quan = prompt("Enter The Quantity","")
while(Number(quan)<0){
quan = prompt("No Negative Numbers, Please Enter Quantity","")
}//end of < 0 QUAN
var pric = prompt("Enter The Price","")
while(Number(pric)< 0){
pric = prompt ("No Negative Numbers, Please Enter Price","")
}// End Of < 0 PRIC

function item(prod, quan, pric){
this.product (prod);
this.quantity (quan);
this.price (pric);

}//End Of Value

} //end of item

document.write(prod+"<br>")
document.write(quan+"<br>")
document.write("$"+pric+"<br>")
</script>

Now KOR has given me this following piece of code that has the exatr components added however as a noob i dont understand it and i need it converted to a simplified form like with functions and stuff if you get me...
also the functions in this will be used with buttons eventually if that helps at all with the coding part?

<script>
var arr=['Product Added :','Quantity : ','Price : ','Total Value : ']
function addItem(){
var prod = prompt('Please Enter Product','');
var quan = prompt('Please Enter Quantity','');
while(Number(quan)<0){
quan = prompt('No negative numbers allowed. Please Enter Quantity','');
}
var pric = prompt('Please Enter Price','');
while(Number(pric)<0){
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');
s.style.fontWeight='bold';
s.appendChild(document.createTextNode(arr[i]))
root.appendChild(s);
root.appendChild(document.createTextNode(myItem[arg]))
root.appendChild(document.createElement('br'))
i++;i==arr.length?root.appendChild(document.createElement('br')):null;
}
confirm('Do you want to add another item?')?addItem():null;
}
onload = addItem;
</script>

<script>

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 + "");

</script>

Hopefully you an get teh general drift of what im trying to do here and help me out, this is so important to me and i just really need some help soon.

Cheers to anyone that can help, especially you KOR, you're probably getting very sick of me.lol

cheers,
--==::SATRAH::==--

Beagle
05-25-2006, 05:26 PM
<script>
var arr=['Product Added :','Quantity : ','Price : ','Total Value : ']

function Item(p_productName, p_quantity, p_price)
{ // *** Use: var myItem = new Item('Name', 5, 15.34); ***
this.product = p_productName;
this.price = Number(p_price);
this.quantity = Number(p_quantity);
this.valueInStock = this.price * this.quantity;
} // *** End Object Definition: Item ***

function addItem()
{
/**************************
*** Local Variables ***/

var prod; // *** Product name inputted by user ***
var quan; // *** Quantity of product inputted by user ***
var pric; // *** Price of product inputted by user ***
var myItem; // *** this is the new object ***
var root; // *** DOM Node for displaying everything ***
var i; // *** Iterator ***
var s; // *** DOM Node for displaying a line ***

/*** End Local Variables ***
*******************************/

prod = prompt('Please Enter Product','');
quan = prompt('Please Enter Quantity','');

while (Number(quan) < 0)
{ // *** Prevent negative quantity ***
quan = prompt('No negative numbers allowed. Please Enter Quantity','');
}

pric = prompt('Please Enter Price','');

while (Number(pric) < 0)
{ // *** Prevent negative price ***
pric = prompt('No negative numbers allowed. Please Enter Price','');
}

myItem = new Item(prod, quan, pric);
root = document.getElementById('mydiv');
i=0;

for (arg in myItem)
{
s=document.createElement('span'); // *** Create a new line ***
s.style.fontWeight='bold';
s.appendChild(document.createTextNode(arr[i])) // *** Add the heading from arr ***
root.appendChild(s); // *** Add the line to the display ***
root.appendChild(document.createTextNode(myItem[arg])) // *** Add the text from myItem ***
root.appendChild(document.createElement('br'))
i++;

if (i == arr.length)
root.appendChild(document.createElement('br'));
}

if (confirm('Do you want to add another item?'))
{ // *** If the user wants to add another, recursively call this function ***
addItem();
}
}

onload = addItem; // *** make the script run at page load ***
</script>

<script>

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 + "");

</script>


I hope this clarifies things a little bit.

I added comments, expanded the needless ternary expressions to make them more readable, and added a constructor for Item to pull the definition of the object out of the user interface code.

What more do you want clarified?



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum