Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

rap name generator script functional #7

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
76 changes: 73 additions & 3 deletions js/script.js
Original file line number Diff line number Diff line change
@@ -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).
Expand All @@ -20,13 +20,83 @@ function Generator() {

}

function getRapName(name, first_names, last_names) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

it feels like these 'generation' methods should probably be behaviors of Generator.

return getRapFirstName(name, first_names) + getRapLastName(name, last_names);
}

function getRapFirstName(name, first_names) {
var numOptions = first_names.length + 2;
var option = getRandomInt(0, numOptions);
if(option > numOptions) {
alert("Error: random number generator is wrong");
}
if(option === numOptions) {
return initializeName(name);
}
else if(option === numOptions-1){
return name;
}
else if(option === numOptions-2) {
return name.toUpperCase()[0];
}
else {
return first_names[option] + " " + name;
}
}

function getRapLastName(name, last_names) {
var numOptions = last_names.length;
var option = getRandomInt(0, numOptions);
if(option > numOptions) {
alert("Error: random number generator is wrong");
}
if(option === numOptions){
return '';
}
else {
return " " + last_names[option];
}
}


//Add your codez here
function initializeName(name) {
var initials = name.toUpperCase().split('');
var newName = '';
initials.forEach( function(letter) {
newName+= letter + ".";
});
return newName;
}

function verifyInput(name) {
return name != "";
}

function getUsersName() {
return $("input[id='user-input']").val();
}

/**
* Gets a random number between min (inclusive) and max (inclusive)
*/
function getRandomInt(min, max) {
return Math.floor(Math.random() * (max - min + 1) + min);
}

$(document).ready(function() {

var engine = new Generator;
//Add your codez here
$("#enter").click( function() {
if(!verifyInput(getUsersName())) {
$(".response").hide();
$(".error").show();
}
else {
var rapName = getRapName(getUsersName(), engine.first_names, engine.last_names)
$(".error").hide();
$(".response").text(rapName);
$(".response").show();
}
});

});