Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
43 changes: 38 additions & 5 deletions js/script.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@


/**
/**
* RAP NAME GENERATOR
* The user will insert their first name and on click receive one of several
* possible outputs (i.e. Jill).
Expand All @@ -12,21 +12,54 @@
* "Inspectah J"
**/

var rnd = function(array) {
return array[Math.floor(Math.random() * array.length)];
}

var capitalize = function(str) {
var ret = "";
for (var i = 0; i < str.length; i++) {
ret += str.charAt(i).toUpperCase() + ".";
}
return ret;
}

function Generator() {

/* Name Arrays: Customize names to change possible output */
this.last_names = ['the Chef', 'Digital', 'Wise', 'Knight', 'Wrecka', 'the Genius', 'the Zoo Keeper', 'the Monk', 'the Scientist', 'the Disciple', 'the Darkman', 'Pellegrino', 'the Ill Figure', 'Rocks The World', 'the Baptist',];
this.first_names = ['Inspectah', 'Masta', 'Poppa', 'Five Foot', 'Ghostface', 'Old Dirty'];

}

Generator.prototype.validate = function(name) {
return name.match(/[A-Z][a-z]*/);
}

//Add your codez here

Generator.prototype.generate = function(name) {
var formats = [
function(name) { return rnd(this.first_names) + " " + name; },
function(name) { return capitalize(name) + " " + rnd(this.last_names); },
function(name) { return rnd(this.first_names) + " " + name + " " + rnd(this.last_names); },
function(name) { return name + " " + rnd(this.last_names); },
function(name) { return rnd(this.first_names) + " " + name.charAt(0).toUpperCase(); }
];
var formatter = rnd(formats).bind(this);

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Check out using apply in this case instead of bind - this way you can do 46 and 47 in one line.

return formatter(name);
}

$(document).ready(function() {

var engine = new Generator;
//Add your codez here

$('#enter').click(function() {
var name = $('#user-input').val();
if (engine.validate(name)) {
$('.response').text(engine.generate(name));
console.log($('.response').text());
$('.response').css('display', 'inline');
} else {
$('.error').css('display', 'inline');
}
});

});