From 04d86caff67d71aa287c81496ba26bc49b09365c Mon Sep 17 00:00:00 2001 From: awanninger Date: Sun, 17 Jan 2016 17:18:40 -0500 Subject: [PATCH 1/2] completed exercise --- js/script.js | 52 ++++++++++++++++++++++++++++++++++++++++++---------- 1 file changed, 42 insertions(+), 10 deletions(-) diff --git a/js/script.js b/js/script.js index 2f2cff7..820157d 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 @@ -12,21 +10,55 @@ * "Inspectah J" **/ -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']; +// add a .sample() function to our array objects +Array.prototype.sample = function() { + // returns a random element from the array + return this[Math.floor((Math.random() * this.length - 1) + 1)]; +} +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.rapName = function(basicName) { + // generate random number between 1 and 2 + var x = Math.floor((Math.random() * 2) + 1); -//Add your codez here + // decide to add first name or last name + if (x == 2) { + return basicName + " " + this.last_names.sample(); + } else { + return this.first_names.sample() + " " + basicName; + } +}; +Generator.prototype.validInput = function(input) { + var pattern = /^ *[a-zA-Z]/g; + // return true if input contains numbers or is empty + return pattern.test(input); +} $(document).ready(function() { + var engine = new Generator; - var engine = new Generator; - //Add your codez here + $('#enter').on('click', function() { + var userName = $('#user-input').val(); + // test user input and display rap name or error + if (engine.validInput(userName)) { + $('.error').hide(); + $('.response').text(engine.rapName(userName)).show(); + } else { + $('.response').hide(); + $('.error').show(); + } + }); }); + From 674ae08245bc781940d9e0445fca732dc6f9fbbb Mon Sep 17 00:00:00 2001 From: awanninger Date: Mon, 18 Jan 2016 16:54:33 -0500 Subject: [PATCH 2/2] updated .sample() function to generate random numbers from 0 to this.length --- js/script.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/js/script.js b/js/script.js index 820157d..044ae90 100644 --- a/js/script.js +++ b/js/script.js @@ -13,7 +13,7 @@ // add a .sample() function to our array objects Array.prototype.sample = function() { // returns a random element from the array - return this[Math.floor((Math.random() * this.length - 1) + 1)]; + return this[Math.floor(Math.random() * this.length)]; } function Generator() {