Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 4 of 4
  1. #1
    New to the CF scene
    Join Date
    May 2006
    Posts
    8
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Exclamation NEED URGENT HELP!!! > Javascript Methods

    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).
    Code:
    <script>
    
    	function Item(prod,quan,pric){
    		this.prod = prod;
    		this.quan = parseInt(quan);
    		this.pric = parseFloat(pric);
    		
    		this.valueInStock = calcValue();
    		this.allDetails
    	
    	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 + ""); 
    
    </script>
    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,
    --==::SATRAH::==--

  • #2
    Kor
    Kor is offline
    Red Devil Mod Kor's Avatar
    Join Date
    Apr 2003
    Location
    Bucharest, ROMANIA
    Posts
    8,478
    Thanks
    58
    Thanked 379 Times in 375 Posts
    I would have used a new Object instead of a new Function
    PHP Code:
    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"  "http://www.w3.org/TR/html4/loose.dtd">
    <
    html>
    <
    head>
    <
    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','');
    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>
    </head>
    <body>
    <div id="mydiv"></div>
    </body>
    </html> 
    KOR
    Offshore programming
    -*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*

  • #3
    New to the CF scene
    Join Date
    May 2006
    Posts
    8
    Thanks
    0
    Thanked 0 Times in 0 Posts
    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
    cheers

  • #4
    Kor
    Kor is offline
    Red Devil Mod Kor's Avatar
    Join Date
    Apr 2003
    Location
    Bucharest, ROMANIA
    Posts
    8,478
    Thanks
    58
    Thanked 379 Times in 375 Posts
    If there are lines/methods you don't understand, please feel free to write them, post them, and I will explain
    KOR
    Offshore programming
    -*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •