From 577215cf04b14cc4ed6bc2d03204b5bfa88a355c Mon Sep 17 00:00:00 2001 From: Justin Holm Date: Tue, 16 May 2017 13:32:21 -0400 Subject: [PATCH 1/2] Justin Holm solution --- index.html | 7 +++--- js/script.js | 69 ++++++++++++++++++++++++++++++++++++++++++++-------- 2 files changed, 62 insertions(+), 14 deletions(-) diff --git a/index.html b/index.html index 18f8b32..c5d54b5 100644 --- a/index.html +++ b/index.html @@ -1,7 +1,6 @@ - - + @@ -69,8 +68,8 @@

Rap Name Generator

- - + + diff --git a/js/script.js b/js/script.js index 2f2cff7..1745305 100644 --- a/js/script.js +++ b/js/script.js @@ -1,6 +1,4 @@ - - -/** +/** * RAP NAME GENERATOR * The user will insert their first name and on click receive one of several * possible outputs (i.e. Jill). @@ -14,19 +12,70 @@ 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']; + /* 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']; -} + var last_names = this.last_names; + var first_names = this.first_names; + + this.rap_name = function() { + return first_name() + ' ' + middle_name() + ' ' + last_name() + } + + this.name = function(original_name) { + if (this.original_name == undefined) original_name = original_name.trim(); + this.original_name = original_name; + }; + + function last_name() { + var index = getRandomIndex(last_names); + return last_names[index]; + } + + function first_name() { + var index = getRandomIndex(first_names); + return first_names[index]; + } + function middle_name(string) { + var options = []; -//Add your codez here + // .charAt() and .toUpperCase() were throwing expections + // with normal this.original_name. String() encasing seemed to fix it + this.original_name = String(this.original_name); + options.push(this.original_name); + options.push(this.original_name.charAt(0).toUpperCase()); + options.push(this.original_name.toUpperCase().split('').join('.')); + + var index = getRandomIndex(options); + return options[index] + } + + function getRandomIndex(array) { + return Math.floor(Math.random() * array.length); + } +} $(document).ready(function() { + var engine = new Generator; + + $("#enter").click(function() { + + if ($("#user-input").val() != "") { + $(".error").hide(); + $(".response").show(); + } else { + $(".error").show(); + $(".response").hide(); + } + + var original_name = $("#user-input").val(); + engine.name(original_name); - var engine = new Generator; - //Add your codez here + var rap_name = engine.rap_name; + $('.response').text(rap_name); + }); }); From 9a09f87827018798447535f3d532387c955f703a Mon Sep 17 00:00:00 2001 From: Justin Holm Date: Tue, 16 May 2017 13:52:10 -0400 Subject: [PATCH 2/2] Justin Holm solution --- js/script.js | 49 +++++++++++++++++++++++++++++++------------------ 1 file changed, 31 insertions(+), 18 deletions(-) diff --git a/js/script.js b/js/script.js index 1745305..58e7827 100644 --- a/js/script.js +++ b/js/script.js @@ -9,7 +9,6 @@ * "Jill the Disciple" * "Inspectah J" **/ - function Generator() { /* Name Arrays: Customize names to change possible output */ @@ -19,14 +18,13 @@ function Generator() { var last_names = this.last_names; var first_names = this.first_names; - this.rap_name = function() { - return first_name() + ' ' + middle_name() + ' ' + last_name() - } + this.generate = function(name) { + rap_name = [] + rap_name.push(first_name(), middle_name(name), last_name()); - this.name = function(original_name) { - if (this.original_name == undefined) original_name = original_name.trim(); - this.original_name = original_name; - }; + shuffled = shuffle(rap_name).join(' '); + return shuffled + } function last_name() { var index = getRandomIndex(last_names); @@ -38,12 +36,12 @@ function Generator() { return first_names[index]; } - function middle_name(string) { + function middle_name(name) { var options = []; // .charAt() and .toUpperCase() were throwing expections // with normal this.original_name. String() encasing seemed to fix it - this.original_name = String(this.original_name); + this.original_name = String(name); options.push(this.original_name); options.push(this.original_name.charAt(0).toUpperCase()); @@ -53,6 +51,24 @@ function Generator() { return options[index] } + function shuffle(array) { + var currentIndex = array.length, tempVal, randomIndex; + + // While there remain elements to shuffle + while (0 !== currentIndex) { + // Pick a remaining element + randomIndex = Math.floor(Math.random() * currentIndex); + currentIndex -= 1; + + // Swap it + tempVal = array[currentIndex]; + array[currentIndex] = array[randomIndex]; + array[randomIndex] = tempVal; + } + + return array; + } + function getRandomIndex(array) { return Math.floor(Math.random() * array.length); } @@ -63,19 +79,16 @@ $(document).ready(function() { $("#enter").click(function() { - if ($("#user-input").val() != "") { + if ($("#user-input").val() !== "") { $(".error").hide(); - $(".response").show(); + + var original_name = $("#user-input").val(); + var rap_name = engine.generate(original_name) + $(".response").text(rap_name).show(); } else { $(".error").show(); $(".response").hide(); } - - var original_name = $("#user-input").val(); - engine.name(original_name); - - var rap_name = engine.rap_name; - $('.response').text(rap_name); }); });