Skip to content

Instantly share code, notes, and snippets.

@Beyamor
Created June 23, 2013 00:43
Show Gist options
  • Save Beyamor/5843270 to your computer and use it in GitHub Desktop.
Save Beyamor/5843270 to your computer and use it in GitHub Desktop.
Twitter name generator
$(function() {
var adjectives = [],
nouns = [];
var any = function(coll) {
var index = Math.floor(Math.random() * coll.length);
return coll[index];
};
var generateName = function() {
var adjective, noun;
do {
adjective = any(adjectives);
noun = any(nouns);
} while (adjective == noun);
return adjective.toUpperCase() + ' ' + noun.toUpperCase();
};
var responsesReceived = 0,
totalNumberOfResponses = 3,
responseReceived = function() {
++responsesReceived;
if (responsesReceived == totalNumberOfResponses) {
var $go = $('<button type="button">Go</button>')
$go.click(function() {
$('#name').html(generateName());
});
$('#loading').remove();
$('body').append($go);
}
};
var requestWords = function(url, process) {
$.ajax(
url, {
dataType: 'text',
success: function(results) {
var words = results
.split(/\n/)
.filter(function(word) { return (word.length > 0); });
process(words);
responseReceived();
},
error: function() {
alert('Something done broke');
}
});
};
requestWords('adjectives', function(words) {
$.each(words, function(_, word) { adjectives.push(word); });
});
requestWords('nouns', function(words) {
$.each(words, function(_, word) { nouns.push(word); });
});
requestWords('both', function(words) {
$.each(words, function(_, word) { adjectives.push(word); nouns.push(word); });
});
});
@ColtonPhillips
Copy link

a) whats going on with the responses received thing here?

b) why do the request functions near bottom not need a var ?

c) is _ a special thing there ?

d) and what the hell do those request functions do anyway. are you just calling requestWords 3 times. ya you are derp.

@ColtonPhillips
Copy link

okay i understand where responseReceived is being called i dont understand what ur accomplishing w it

@ColtonPhillips
Copy link

what is $go.click. some callback function for clicking on the element/

@ColtonPhillips
Copy link

you remove loading but where is that even added

@ColtonPhillips
Copy link

looks like the callback expects multiple words? splittong on /n so a list of em okay

@ColtonPhillips
Copy link

looks like the callback expects multiple words? splittong on /n so a list of em okay

@ColtonPhillips
Copy link

are these success and error tags some sort of generic thing going on here?

@ColtonPhillips
Copy link

nanananananananna batman

@ColtonPhillips
Copy link

generic as in like, i dunno... you can set em up for different purposes for libraries, as opposed to hard coded try catch in python. maybe a colton library could have like

chince:
//
dismember:
//
fart:
//

@ColtonPhillips
Copy link

or no im dumb those aren't labels they are dict items passed to the ajax ?

@ColtonPhillips
Copy link

hope you dont get like 12 emails.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment