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
44 changes: 38 additions & 6 deletions js/script.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,18 +15,50 @@
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'];
this.last_names = ['The Dill', '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 = ['Fella', 'Inspectah', 'Masta', 'Poppa', 'Five Foot', 'Ghostface', 'Old Dirty'];

this.generate = function() {
Copy link
Member

Choose a reason for hiding this comment

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

What's the benefit of defining these methods on the prototype instead?

Generator.prototype.generate = function (){
  //codez here
}

Copy link
Author

Choose a reason for hiding this comment

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

Admittedly, I wasn't entirely certain about the benefits of defining the method on the prototype instead of the constructor. I would normally just use the constructor for defining all attributes and methods on my objects. So I looked it up and I found this discussion on StackOverflow.

So there are two benefits:

  1. Efficiency => The JS runtime keeps a single copy of method for all Generator objects
  2. Stickiness => If the method gets redefined later during execution after the objects are created, the new method definition applies to all existing Generator objects.

Interesting.

var firstName = this.first_names[generateRandomindex(this.first_names)];
var lastName = this.last_names[generateRandomindex(this.last_names)];

return firstName + " " + lastName;
}
}

function generateRandomindex(array) {
return Math.floor(Math.random() * array.length);
}

$(document).ready(function() {

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

$("#enter").click(function() {
var name = $("#user-input").val().trim();
if (name.length == 0) {
showDanger(".alert-success", ".alert-danger");
return;
}
var rapName = engine.generate();
$(".alert-success").text(generateMessage(name, rapName));
showSuccess(".alert-success", ".alert-danger");

});

$(document).ready(function() {
function showSuccess(successSelector, dangerSelector) {
$(dangerSelector).css('display', 'none');
$(successSelector).css('display', 'block');
}

var engine = new Generator;
//Add your codez here
function showDanger(successSelector, dangerSelector) {
$(dangerSelector).css('display', 'block');
$(successSelector).css('display', 'none');
}

function generateMessage(name, rapName) {
Copy link
Member

Choose a reason for hiding this comment

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

Nice addition!

var escaped = escape(name).split("%20").join(" ");
return escaped + ", your rap name is " + rapName + "!";
}

});