...

View Full Version : Simple javascript loop query for a beginner



Albana_12
05-23-2012, 09:53 PM
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


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

}

felgall
05-23-2012, 10:28 PM
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.

Albana_12
05-24-2012, 04:08 AM
Can you please, please be more specific? Like, tell me how i should do it...
Please!

Old Pedant
05-24-2012, 06:12 AM
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.


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

Logic Ali
05-24-2012, 09:13 AM
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.



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

i++;
}

Old Pedant
05-24-2012, 05:34 PM
LOL! SHEESH! I hereby qualify as a certified idiot.

My face is burning, truly! Embarassed doesn't cover it.



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum