Thread: Most efficient way to do this?

Results 1 to 4 of 4
  1. #1 Most efficient way to do this? 
    Registered Member Andrew's Avatar
    Join Date
    Nov 2008
    Posts
    2,890
    Thanks given
    612
    Thanks received
    207
    Rep Power
    551
    So I am currently developing an App and part of it is in in JavaScript (yay for me)

    Basically lets say i have this


    Code:
    function pets (breed, name, age) {
    this.breed = breed;
    this.name = name;
    this.age = age;
    }
    then i have this

    Code:
    var differentPets = Observable(
    
    new pets(breed, name, age);
    new pets(breed, name, age);
    );
    So i have these displayed using the front-end markup of this service i am using and I am able to display all the pets by doing the equivilent of an for() function except in this other language. Now I am trying to make it so when I click on one of these pets, i can just console.log(); the name, breed, age etc... But how would i go about that? I would need to create a completely different function to check to see what pet it is like so:

    Code:
    function checkPet() {
    }
    But how can i make it so that the computer knows it is that... I was thinking to have a boolean within the original consutrctor for pets and then when it is clicked change the value of the boolean and call the function to search to see which of the pets were clicked, but is that efficient? or is there an easier way to do it?
    Reply With Quote  
     

  2. #2  
    Renown Programmer
    Greg's Avatar
    Join Date
    Jun 2010
    Posts
    1,179
    Thanks given
    260
    Thanks received
    1,012
    Rep Power
    2003
    Depends how the pet information is stored, you could give each pet an id and when one is clicked a variable is set to that id, you can use that variable to get information about the pet.
    Attached imageAttached image
    Reply With Quote  
     

  3. #3  
    Registered Member Andrew's Avatar
    Join Date
    Nov 2008
    Posts
    2,890
    Thanks given
    612
    Thanks received
    207
    Rep Power
    551
    Quote Originally Posted by Greg View Post
    Depends how the pet information is stored, you could give each pet an id and when one is clicked a variable is set to that id, you can use that variable to get information about the pet.
    that's kind of what I was thinking except in regards to a true/false statement; how would i set the id though? Is that any different than just a simple boolean? (i'm still in the learning process)
    Reply With Quote  
     

  4. #4  
    Renown Programmer
    Greg's Avatar
    Join Date
    Jun 2010
    Posts
    1,179
    Thanks given
    260
    Thanks received
    1,012
    Rep Power
    2003
    Quote Originally Posted by Andrew View Post
    that's kind of what I was thinking except in regards to a true/false statement; how would i set the id though? Is that any different than just a simple boolean? (i'm still in the learning process)
    A boolean has only two states true (1) or false (0), so you could give all the buttons a boolean and turn to true if clicked the loop through them all and check the boolean however that's not particularly efficient.

    If you change
    Code:
    function pets (id, breed, name, age) {
    this.id = id;
    this.breed = breed;
    this.name = name;
    this.age = age;
    }
    and give every pet a unique id

    then create a variable
    Code:
    var clickedPetId = 0;
    Then when a pet is clicked you can simply set
    Code:
    clickPetId = id;
    then use it to gain the info you need.

    It requires more storage space but is more efficient when it comes to processing as there is no need to loop through all the animals.
    Although unless you're loading millions of animals I doubt it will make a negligible difference.
    Attached imageAttached image
    Reply With Quote  
     

  5. Thankful user:



Thread Information
Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)


User Tag List

Similar Threads

  1. A More "Efficient" Way To Doing This
    By Gluon in forum Requests
    Replies: 2
    Last Post: 01-24-2012, 10:22 PM
  2. Easier way to do this
    By Kenneh in forum Application Development
    Replies: 23
    Last Post: 06-17-2010, 11:40 PM
  3. Fastest way to do this?
    By Ultimate in forum RS2 Server
    Replies: 11
    Last Post: 01-21-2010, 08:04 PM
  4. Any way to do this?
    By arr0wtohell in forum Help
    Replies: 3
    Last Post: 12-29-2009, 04:41 AM
  5. Replies: 14
    Last Post: 03-15-2009, 11:07 AM
Posting Permissions
  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •