From 1360ef3a984a6adb740ee463a147d6c9c521847c Mon Sep 17 00:00:00 2001 From: Peter Mueller Date: Mon, 1 Jun 2015 00:30:51 -0400 Subject: [PATCH] add Generator functions and AJAX for Rap Name gen --- index.html | 18 +++++++------- js/script.js | 66 ++++++++++++++++++++++++++++++++++++++++++++++++---- 2 files changed, 71 insertions(+), 13 deletions(-) diff --git a/index.html b/index.html index 18f8b32..404d39a 100644 --- a/index.html +++ b/index.html @@ -9,12 +9,12 @@ Wu-Tang Clan Rap Name Generator - + - + @@ -42,15 +42,15 @@
- +
-

Rap Name Generator

-

Do you want a crazy cool rap name like your favorite
middle-aged rapper from the 90's supergroup Wu-Tang Clan?

Enter your real first name below and click play!

+

Rap Name Generator

+

Do you want a crazy cool rap name like your favorite
middle-aged rapper from the 90's supergroup Wu-Tang Clan?

Enter your real first name below and click play!

- +
- - + + diff --git a/js/script.js b/js/script.js index 2f2cff7..daafdb0 100644 --- a/js/script.js +++ b/js/script.js @@ -1,6 +1,6 @@ -/** +/** * RAP NAME GENERATOR * The user will insert their first name and on click receive one of several * possible outputs (i.e. Jill). @@ -20,13 +20,71 @@ function Generator() { } +Generator.prototype.toAcronym = function(name) { + return name.split("").join(".").toUpperCase()+"." +} + +Generator.prototype.toInitial = function(name) { + return name[0].toUpperCase() +} -//Add your codez here +Generator.prototype.append = function(name) { + return name + " " + this.last_names[Math.floor(Math.random() * this.last_names.length)] +} +Generator.prototype.prepend = function(name) { + return this.first_names[Math.floor(Math.random() * this.first_names.length)] + " " + name +} + +Generator.prototype.sandwich = function(name) { + return this.prepend(this.append(name)) +} + +Generator.prototype.onlyLetters = function(name) { + return name.replace(/[^a-zA-Z]/g,"") +} + +Generator.prototype.generate = function(name) { + var new_name; + switch (Math.floor(Math.random() * 3)) { + case 0: + new_name = name; + break; + case 1: + new_name = this.toAcronym(name); + break; + case 2: + new_name = this.toInitial(name); + break; + } + switch (Math.floor(Math.random() * 3)) { + case 0: + new_name = this.append(new_name); + break; + case 1: + new_name = this.prepend(new_name); + break; + case 2: + new_name = this.sandwich(new_name); + break; + } + return new_name; +} $(document).ready(function() { - var engine = new Generator; - //Add your codez here + var engine = new Generator; + + $('#enter').on('click', function(){ + var input = engine.onlyLetters($('#user-input').val()) + if (input !== "") { + $('.error').hide() + $('.response').html(engine.generate(input)).show() + } else { + $('.error').show() + $('.response').hide() + } + }); + });