From c72729a0f1fd80fa5c00e51f70c15c8ef926fa8c Mon Sep 17 00:00:00 2001 From: Tirthankar Bhattacharjee Date: Mon, 5 Oct 2015 21:55:49 -0400 Subject: [PATCH 1/2] add generator code for randomizing names --- js/script.js | 22 ++++++++++++++++------ 1 file changed, 16 insertions(+), 6 deletions(-) diff --git a/js/script.js b/js/script.js index 2f2cff7..08b4e7b 100644 --- a/js/script.js +++ b/js/script.js @@ -1,5 +1,3 @@ - - /** * RAP NAME GENERATOR * The user will insert their first name and on click receive one of several @@ -13,20 +11,32 @@ **/ 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.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.generateName = function(actualName) { + var firstname = this.getRandomName(this.first_names); + var lastname = this.getRandomName(this.last_names); + var maybeMiddle = Math.floor((Math.random() * 2) + 0) == 0 ? actualName + " " : ""; + return firstname + " " + maybeMiddle + lastname; } +Generator.prototype.getRandomPosition = function(array) { + return Math.floor((Math.random() * array.length) + 0); +} -//Add your codez here +Generator.prototype.getRandomName = function(names) { + return names[this.getRandomPosition(names)]; +} + //Add your codez here $(document).ready(function() { var engine = new Generator; - //Add your codez here + var name = engine.generateName("Tirth"); + console.log(name); }); From a107a5b246f7f6e51cfd961571f9e505abf1ab13 Mon Sep 17 00:00:00 2001 From: Tirthankar Bhattacharjee Date: Mon, 5 Oct 2015 22:21:10 -0400 Subject: [PATCH 2/2] separate app code from generator object --- index.html | 1 + js/app.js | 22 ++++++++++++++++++++++ js/script.js | 10 ---------- 3 files changed, 23 insertions(+), 10 deletions(-) create mode 100644 js/app.js diff --git a/index.html b/index.html index 18f8b32..6e923ce 100644 --- a/index.html +++ b/index.html @@ -73,5 +73,6 @@

Rap Name Generator

+ diff --git a/js/app.js b/js/app.js new file mode 100644 index 0000000..3e8dc5b --- /dev/null +++ b/js/app.js @@ -0,0 +1,22 @@ +$(document).ready(function() { + setFormBindings(); +}); + +function setFormBindings() { + $('#enter').on('click', function() { + var user_input = $('#user-input').val(); + if (user_input) { + var engine = new Generator; + var name = engine.generateName(user_input); + $('.response').html(name).show(); + } else { + $('.error').show(); + } + }); + + $('#user-input').focus(function() { + $('.error').hide(); + $('.response').html("").hide(); + }); + +} diff --git a/js/script.js b/js/script.js index 08b4e7b..f60301b 100644 --- a/js/script.js +++ b/js/script.js @@ -30,13 +30,3 @@ Generator.prototype.getRandomPosition = function(array) { Generator.prototype.getRandomName = function(names) { return names[this.getRandomPosition(names)]; } - //Add your codez here - - -$(document).ready(function() { - - var engine = new Generator; - var name = engine.generateName("Tirth"); - console.log(name); - -});