Jump to content

Creating a semi-random name generator (newbie at javascript!)


namegame

Recommended Posts

Let me know if any of this is confusing and I’ll try to clarfiy!I’d like to make a semi-random name generator. The names are made from three parts: [first] of the [second] [third], like [Flash] of the [sapphire] [sky]. What makes the names complicated is that they need to have at least one nature-related word in them in any of the three parts, like Beach of Summer’s Joy (“Beach” is the nature word, in the first part), Dedication of the Maple’s Colors (“Maple,” in the second), or Anger of the Rolling Ocean (“Ocean” here, in the third part). Thanks to online-generator.com, I was able to make a completely random generator without a problem; that generator pulled words from three sets and pieced them together. But, being random, it created names that didn’t follow the rule of having a nature term in there. Duh.After playing around with a few generators, I think I’ve figured out a way to accomplish this. I have four variable sets: one for each part of the name, and then a fourth set that will include the nature terms. And then here’s where I get stuck. I can’t come up with and/or borrow code that will instruct the generator to swap in the fourth variable set for one of the three other sets.So my question is, how do I do that? Which is linked to other questions: Is this possible? Am I coding it correctly or do I need a different approach? I’m hoping that someone can at least point me in the right direction! Again, please let me know if this question or post doesn’t make sense.Here’s my current and incomplete code, minus the vast majority of terms in the variable sets (and the style sheets and whatnot, to save space):

<html><head>    <script type="text/javascript">           function generator(){                  var wordlist1 = ["Abhorrence","Acclamation","Adoration","Adornment",,"Artist","Ash","Ashes","Attacker","Aurora","Yearning","Zenith","Zephyr"];           var wordlist2 = ["of Mythic","of the Abhorrent","of the Accursed","of the Adorned","of the Amber","of the Amber","of the Amethyst","of the Ancient","of the Approaching","of the Aquatic","of the Wretched","of the Xanthic","of the Yellow"];var wordlist3 = ["Adornment","Air","Alignment","Amber","Asylum","Attacker","Aurora","Autumn","Awe","Bands","Basilisk","Battle","Beach","Bedrock","Blaze","Blood","Blossom","Wound","Wraith","Wreath","Zenith","Zephyr"]var wordlist4 = ["Amber","Aurora","Autumn","Beach","Bedrock","Blossom","Blossoms","Bough","Branch","Breeze","Breezes","Briar","Brine","Charcoal"]                   // Random numbers are made            var randomNumber1 = parseInt(Math.random() * wordlist1.length);            var randomNumber2 = parseInt(Math.random() * wordlist2.length);            var randomNumber3 = parseInt(Math.random() * wordlist3.length);            var randomNumber4 = parseInt(Math.random() * wordlist4.length);            var name = wordlist1[randomNumber1] + " " + wordlist2[randomNumber2] + " " + wordlist3[randomNumber3];                                  //alert(name); //Remove first to slashes to alert the name                       //If there's already a name it is removed              if(document.getElementById("result")){                document.getElementById("placeholder").removeChild(document.getElementById("result"));            }           // A div element is created to show the generated name. The Name is added as a textnode. Textnode is added to the placeholder.            var element = document.createElement("div");            element.setAttribute("id", "result");            element.appendChild(document.createTextNode(name));            document.getElementById("placeholder").appendChild(element);        }   </script>

Thank you! I’m new to coding, so please excuse my ignorance. This was supposed to be a fun test for myself and a little gift for a friend, but I had no idea how complicated it’d be for me.

Edited by namegame
Link to comment
Share on other sites

It looks to me as if you are close to a workable solution, although I don't really understand what you are wanting to do at the bottom of the code where you are generating a div with some sort of content.

 

I don't know how well "Abhorrence of Mythic Adornment Amber" works as a name, but you can tune those noun and adjective strings until they work.

Link to comment
Share on other sites

Thanks for your reply!

 

I'm sorry - I didn't explain myself well. I'm pretty new to coding and this is a messy quesiton. The name's supposed to have three parts - so "Abhorrence of Mythic Adornment" or "Abhorrence of Mythic Amber," not four as in "Abhorrence of Mythic Adornment Amber." It's a quirk of the names in my friend's fantasy world, and that's what makes the generator more complicated than your average bear.

 

So that fourth variable set - var randomNumber4 = parseInt(Math.random() * wordlist4.length); - I was hoping to swap in for one of the other three variable sets. Essentially, I wanted to tell the generator: If you use a nature word for the first part of the name, then use any word for the second part and any word for the third part; if you use any word for the first part, you need to use a nature word for the second part or a nature word for the third part. Another way to state this: If you use a word from variable set one for the first part of the name, then you need to use a word from variable set four for the second part or for the third part.

 

So, like (and this isn't real code)...

 

 

var name = wordlist4 + " " + wordlist2[randomNumber2] + " " + wordlist3[randomNumber3];

OR

var name = wordlist1[randomNumber1] + " " + wordlist4 + " " + wordlist3[randomNumber3];

OR

var name = wordlist1[randomNumber1] + " " + wordlist2[randomNumber2] + " " + wordlist4;

 

But I just can't figure out how to convery those instructions in javascript. Argh so annoying.

 

Regarding that div element, I probably should have just cut that out. I think that's part of the instructions for displaying the result... could be wrong, though.

Link to comment
Share on other sites

Perhaps you just need three paths...

 

var i = parseInt(Math.random() * 3);

if (i==0){

var name = wordlist4[randomNumber] + " " + wordlist2[randomNumber] + " " + wordlist3[randomNumber];

}else if (i==1){

var name = wordlist1[randomNumber] + " " + wordlist4[randomNumber] + " " + wordlist3[randomNumber];

}else if(i==2){

var name = wordlist1[randomNumber] + " " + wordlist2[randomNumber] + " " + wordlist4[randomNumber];

}else{

alert('error');

}

 

Or if you are saying that some of the lists contain two types of words then break them up into two arrays, nature and non-nature.

  • Like 1
Link to comment
Share on other sites

Perhaps you just need three paths...

 

var i = parseInt(Math.random() * 3);

if (i==0){

var name = wordlist4[randomNumber] + " " + wordlist2[randomNumber] + " " + wordlist3[randomNumber];

}else if (i==1){

var name = wordlist1[randomNumber] + " " + wordlist4[randomNumber] + " " + wordlist3[randomNumber];

}else if(i==2){

var name = wordlist1[randomNumber] + " " + wordlist2[randomNumber] + " " + wordlist4[randomNumber];

}else{

alert('error');

}

 

Or if you are saying that some of the lists contain two types of words then break them up into two arrays, nature and non-nature.

 

Yes! That looks like exactly what I need! Thank you so much for your help!

Link to comment
Share on other sites

Thank you so much for your help, davej. I finally have something I can work with!

 

I'm going to take advantage of your kindness, but I hope maybe you (or anyone else! I need all the help I can get!) can find where I'm going wrong now. I've been playing around with that chunk of javascript for nearly 2 hours and haven't gotten my generator to work yet. Here are those three paths inserted into the code that I'd been working on:

 

 

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd"> <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en"><head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <title>Ntest</title> <script type="text/javascript"> /* Online Name Generators v.1.0 script is made by Niels Gamborg webmaster at online-generator.com. * The script is free (as in "free speech" and also as in "free beer") in anyway. Use it after you own likings. * Peace and love. */ function generator(){ var wordlist1 = ["1Abhorrence","1Acclamation","1doration","1Adornment","1Alignment","1Dancer","1Dangling","1Dasher","1Dawn","1Dawning","1Day","1Days","1Death","1Decay","1Deepening","1Deliberation","1Delirium","1Denial","1Depths"]; var wordlist2 = ["2Alabaster","2Apricot","2Argent","2Aureolin","2Aurulent","2Azure","2Burgundy","2Burnt","2Caramel","2Carnelian","2Celadon","2Cerise","2Cesious","2Charcoal","2Chartreuse"];var wordlist3 = ["3Ice","3Iceberg","3Ichor","3Icicle","3Illumination","3Illusion","3Illustration","3Image","3Imagination","3Ingot","3Intonation","3Island","3Isle"];var wordlist4 = ["4Oasis","4Ocean","4Opal","4Orchard","4Panoply","4Peach","4Peaches","4Pearl","4Pebble","4Pines","4Pool","4Pond","4Rain"]var i = parseInt(Math.random() * 3);if(i==0) {var name = wordlist4[randomNumber4] + " of the " + wordlist2[randomNumber2] + " " + wordlist3[randomNumber3];}else if(i==1) {var name = wordlist1[randomNumber1] + " of the " + wordlist4[randomNumber4] + " " + wordlist3[randomNumber3];}else if(i==2) {var name = wordlist1[randomNumber1] + " of the " + wordlist2[randomNumber2] + " " + wordlist4[randomNumber4];

}else{

alert('error');

}

if(document.getElementById("result")){ document.getElementById("placeholder").removeChild(document.getElementById("result")); } // A div element is created to show the generated name. The Name is added as a textnode. Textnode is added to the placeholder. var element = document.createElement("div"); element.setAttribute("id", "result"); element.appendChild(document.createTextNode(name)); document.getElementById("placeholder").appendChild(element);} </script> </head><body onload="generator()"> <div id="wrapper"> <p>name test test test</p> <p> <div id="generator" > <div id="placeholder"></div> <input type="button" value="Make new name" onclick="generator()" /> </div> </p> <div id="footer"> <p>This generator is developed with help from the <a href="http://online-generator.com">online name generators</a>. </p> </div> </div></body></html>

 

So it's not working and I've been developing a migraine trying to figure out why. I've been comparing it to the totally random generator that I *did* get to work, but I really don't know why the totally random one is junctional why this one isn't.

 

Thanks to davej, this is so, so close to being done. And then I can give this up and go back to safety of simple HTML coding.

Edited by namegame
Link to comment
Share on other sites

For comparison, here's the completely random generator code that does work:

 

 

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd"> <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en"><head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <title>Name Generator Script Demo</title> <meta name="description" content="A do it yourself name generator from online-generators.com" /> <!-- The Generator script. You can move the script to an external file for better overview --> <script type="text/javascript"> /* Online Name Generators v.1.0 script is made by Niels Gamborg webmaster at online-generator.com. * The script is free (as in "free speech" and also as in "free beer") in anyway. Use it after you own likings. * Peace and love. */ function generator(){ var wordlist1 = ["1Abhorrence","1Acclamation","1doration","1Adornment","1Alignment","1Dancer","1Dangling","1Dasher","1Dawn","1Dawning","1Day","1Days","1Death","1Decay","1Deepening","1Deliberation","1Delirium","1Denial","1Depths"]; var wordlist2 = ["2Cerise","2Cesious","2Charcoal","2Chartreuse","2Cherry","2Chestnut","2Cinnabar","2Cinnamon","2Citrine","2Claret","2Cobalt","2Cornflower","2Crimson","2Cyan"];var wordlist3 = ["3Ice","3Iceberg","3Ichor","3Icicle","3Illumination","3Illusion","3Illustration","3Jungle","3Key","3Killer","3Labyrinth","3Lace","3Lament","3Laughter","3Leaf","3Leaves","3Legend","3Life","3Light","3Lightning","3Lily"];var wordlist4 = ["4Oasis","4Ocean","4Opal","4Orchard","4Panoply","4Peach","4Peaches","4Pearl","4Pebble","4Pines","4Pool","4Pond","4Rain","4Rainbow","4Rainfall","4Rainforest","4Reef","4River","4Rock","4Rose"]// Random numbers are made var randomNumber1 = parseInt(Math.random() * wordlist1.length); var randomNumber2 = parseInt(Math.random() * wordlist2.length); var randomNumber3 = parseInt(Math.random() * wordlist3.length); var randomNumber4 = parseInt(Math.random() * wordlist4.length); var name = wordlist1[randomNumber1] + " " + wordlist2[randomNumber2] + " " + wordlist3[randomNumber3] + " " + wordlist4[randomNumber4]; //alert(name); //Remove first to slashes to alert the name //If there's already a name it is removed if(document.getElementById("result")){ document.getElementById("placeholder").removeChild(document.getElementById("result")); } // A div element is created to show the generated name. The Name is added as a textnode. Textnode is added to the placeholder. var element = document.createElement("div"); element.setAttribute("id", "result"); element.appendChild(document.createTextNode(name)); document.getElementById("placeholder").appendChild(element); } </script> </head><body onload="generator()"> <div id="wrapper"> <!-- The generator result and button. Change text on the button by changing button value. Be carefull changing ID's in this part --> <div id="generator" > <div id="placeholder"></div> <input type="button" value="Make name" onclick="generator()" /> </div> <div id="footer"> <p>This generator is developed with help from the <a href="http://online-generator.com">online name generators</a>. </p> </div> </div></body></html>

Edited by namegame
Link to comment
Share on other sites

Yes, you have to go back and look at your earlier code.

randomNumber4

...needs to be replaced with...

Math.floor(Math.random() * wordlist4.length)

...etc...

 

--EDIT--

 

Note that Math.floor() is probably a better choice than parseInt() because it is much faster.

Edited by davej
Link to comment
Share on other sites

Yes! And it works now - Thank you so much! I really appreciate your patience and willingness to walk me through this.

 

Thanks again!

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...