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..58e7827 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). @@ -11,22 +9,86 @@ * "Jill the Disciple" * "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']; + /* 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.generate = function(name) { + rap_name = [] + rap_name.push(first_name(), middle_name(name), last_name()); + + shuffled = shuffle(rap_name).join(' '); + return shuffled + } + + 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(name) { + var options = []; + // .charAt() and .toUpperCase() were throwing expections + // with normal this.original_name. String() encasing seemed to fix it + this.original_name = String(name); -//Add your codez here + 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 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); + } +} $(document).ready(function() { + var engine = new Generator; + + $("#enter").click(function() { + + if ($("#user-input").val() !== "") { + $(".error").hide(); - var engine = new Generator; - //Add your codez here + var original_name = $("#user-input").val(); + var rap_name = engine.generate(original_name) + $(".response").text(rap_name).show(); + } else { + $(".error").show(); + $(".response").hide(); + } + }); });