SitePoint Sponsor

User Tag List

Results 1 to 4 of 4
  1. #1
    SitePoint Member
    Join Date
    Dec 2004
    Location
    The Netherlands
    Posts
    4
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Question Prototype issues.

    I'm attempting to figure out the prototype option for javascript to make it easier on myself when adding profiles, except now I'm stuck with a problem

    HTML Code:
    <script language="Javascript">
    
    function trecords() {
    	this.index = (trecords.count++);
    	this.keywords = '';
    	this.total = 0;
    	this.description = '';
    	return this;
    }
    trecords.prototype.set = function(keywords, total, description) {
    	this.keywords = keywords;
    	this.total = total;
    	this.description = description;
    }
    trecords.prototype.searchstring = function() { return this.keywords+' '+this.total+' '+this.description }
    trecords.prototype.count = 0;
    
    function add(keywords,total,description) {
    	al = records.length;
    	records[al] = new trecords();
    	records[al].set(keywords,total,description);
    }
    records = new Array();
    
    add("name", 910, "description");
    add("name", 90, "description");
    add("name", 70, "description");
    
    </script>
    The problem I have is that I need to write a function that will add up records[x].total [so: 910+90+70] and display the total, without having to write it out as:

    records[0].total + records[1].total + ...etc

    Is it at all possible to do so, and if so, could one of you masters help me out? Thanks in advance.

  2. #2
    I'll take mine raw silver trophy MikeFoster's Avatar
    Join Date
    Dec 2002
    Location
    Alabama, USA
    Posts
    2,560
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Here's one way, there are others.
    Code:
    <html>
    <head>
    <script type='text/javascript'>
    function tRecord(keywords, total, description)
    {
      tRecord.data[tRecord.data.length] = this;
      this.set(keywords, total, description);
    }
    
    tRecord.data = new Array(); // static property
    
    tRecord.prototype.set = function(keywords, total, description)
    {
      this.keywords = keywords;
      this.total = total;
      this.description = description;
    }
    
    tRecord.prototype.searchstring = function()
    {
      return this.keywords + this.total + this.description;
    }
    
    tRecord.prototype.sum = function()
    {
      var s = 0;
      for (var i = 0; i < tRecord.data.length; ++i) {
        s += tRecord.data[i].total;
      }
      return s;
    }
    
    //---------------
    
    var db = new tRecord("name1", 910, "description1");
    new tRecord("name2", 90, "description2");
    new tRecord("name3", 70, "description3");
    
    alert(db.sum());
    </script>
    </head>
    <body>
    </body>
    </html>

  3. #3
    SitePoint Member
    Join Date
    Dec 2004
    Location
    The Netherlands
    Posts
    4
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    That one works perfectly. I thank thee, grand master.

  4. #4
    I'll take mine raw silver trophy MikeFoster's Avatar
    Join Date
    Dec 2002
    Location
    Alabama, USA
    Posts
    2,560
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    You're very welcome


Bookmarks

Posting Permissions

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