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 6 of 6
  1. #1
    New Coder
    Join Date
    Dec 2011
    Posts
    26
    Thanks
    4
    Thanked 0 Times in 0 Posts

    Simple javascript loop query for a beginner

    Please i've been having this problem for 2 days now.. i cannot solve it.. i know it's something basic but i can't seem to solve it..

    I need to query all friends from facebook and i need to post just those who have gender==male.

    I do it and it returns only one result.. while i have more than 50 male friends..

    Help me

    Code:
         FB.api('/me/friends?fields=name,first_name,gender', function(response) {
                console.log(response);
                var divContainer=$('.facebook-friends');
                                 var testdiv = document.getElementById("test");
        var i=0;
        
        while (i<response.data.length && response.data[i].gender=='male')
        
                    {
                         testdiv.innerHTML +=  response.data[i].first_name + '<br/>' ;
          i++;
        
                }

  • #2
    Master Coder felgall's Avatar
    Join Date
    Sep 2005
    Location
    Sydney, Australia
    Posts
    6,642
    Thanks
    0
    Thanked 649 Times in 639 Posts
    while (i<response.data.length && response.data[i].gender=='male')

    stops when either of two conditions applies

    1. i not less than response.data.length
    2. response.data[i].gender contains 'male'

    It therefore stops at the first male entry and will only reach the end of the array if everyone in it is female.

    You need to move the second condition to an if statement inside the loop to capture all male entries instead of just the first one.
    Stephen
    Learn Modern JavaScript - http://javascriptexample.net/
    Helping others to solve their computer problem at http://www.felgall.com/

    Don't forget to start your JavaScript code with "use strict"; which makes it easier to find errors in your code.

  • #3
    New Coder
    Join Date
    Dec 2011
    Posts
    26
    Thanks
    4
    Thanked 0 Times in 0 Posts
    Can you please, please be more specific? Like, tell me how i should do it...
    Please!

  • #4
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    25,965
    Thanks
    79
    Thanked 4,429 Times in 4,394 Posts
    I don't know how he could have been more specific. If you wrote that code you should be able to alter it that much.
    Code:
        while (i<response.data.length)
        {
            if (response.data[i].gender=='male')
            {
                testdiv.innerHTML +=  response.data[i].first_name + '<br/>' ;
                i++;
            }
        }
    An optimist sees the glass as half full.
    A pessimist sees the glass as half empty.
    A realist drinks it no matter how much there is.

  • #5
    Senior Coder Logic Ali's Avatar
    Join Date
    Sep 2010
    Location
    London
    Posts
    1,028
    Thanks
    0
    Thanked 207 Times in 202 Posts
    Quote Originally Posted by Old Pedant View Post
    Code:
        while (i<response.data.length)
        {
            if (response.data[i].gender=='male')
            {
                testdiv.innerHTML +=  response.data[i].first_name + '<br/>' ;
                i++;
            }
        }
    Methinks a female could bring a halt to proceedings there.

    Code:
        while (i<response.data.length)
        {
            if (response.data[i].gender=='male')
            {
                testdiv.innerHTML +=  response.data[i].first_name + '<br/>' ;          
            }
    
            i++;
        }

  • #6
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    25,965
    Thanks
    79
    Thanked 4,429 Times in 4,394 Posts
    LOL! SHEESH! I hereby qualify as a certified idiot.

    My face is burning, truly! Embarassed doesn't cover it.
    An optimist sees the glass as half full.
    A pessimist sees the glass as half empty.
    A realist drinks it no matter how much there is.


  •  

    Tags for this Thread

    Posting Permissions

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