Javascript coding help?

Mary

lemon girl 🍋
Joined
Oct 23, 2012
Posts
5,645
Bells
1,300
Cosmic Radioactive Orbiting Spectral #20
Pink Star Fragment
The Bell Tree Fair 2020 Patch
Pink Summer Shell
Kirby Easter Egg
Mother's Day Carnation
Zipper Sakura
Spring Sakura
Spring Sakura
Spring Sakura
Spring Sakura
Spring Sakura
So I'm working on creating a name generator for New Horizons Island names to include on a website I'm making. I haven't finished the wordlist yet, so those are just presets. I want to modify this code to set a max character limit of 8 for the output, but am unsure how. I'd appreciate any help/suggestions, as I only know some HTML and no javascript.
<script type="text/javascript">

function generator(){

var wordlist1 = ["Cool","Masked","Bloody","Lame","Big","Stupid","Drunk","Rotten",
"Blue","Black","White","Red","Purple","Golden","Silver"];

var wordlist2 = ["Hamster","Moose","Lama","Duck","Bear","Eagle","Tiger",
"Rocket","Bullet","Knee","Foot","Hand"]

var randomNumber1 = parseInt(Math.random() * wordlist1.length);
var randomNumber2 = parseInt(Math.random() * wordlist2.length);
var name = wordlist1[randomNumber1] + " " + wordlist2[randomNumber2];


if(document.getElementById("result")){
document.getElementById("placeholder").removeChild(document.getElementById("result"));
}
var element = document.createElement("div");
element.setAttribute("id", "result");
element.appendChild(document.createTextNode(name));
document.getElementById("placeholder").appendChild(element);
}
</script>
 
I'm not sure what way you want to take to modify the combined word (of list 1 and 2), but here's some simple codes you may need. Hope it helps.

How to check the length of text :

var str1 = "Mary";
var str2 = "from TBT";
var str3 = str1 + " " + str2;
var num1 = Number(str3.length);
if (num1>8){
// the process when it's bigger than 8.
} else {
// the process when it's smaller than 8
}

How to combine the words picked up randomly from 2 word lists
var wordlist1 = ["option1", "option2", "option3"];
var wordlist2 = ["optionA", "optionB", "optionC"];

for (var i = 0; i < 10; i++){
var index1 = Math.floor(Math.random() * 3);
var index2 = Math.floor(Math.random() * 3);
var word0 = wordlist1[index1] + " " + wordlist2[index2];
// the process to the word that's generated randomly
}
 
I'm not sure what way you want to take to modify the combined word (of list 1 and 2), but here's some simple codes you may need. Hope it helps.

How to check the length of text :

var str1 = "Mary";
var str2 = "from TBT";
var str3 = str1 + " " + str2;
var num1 = Number(str3.length);
if (num1>8){
// the process when it's bigger than 8.
} else {
// the process when it's smaller than 8
}

How to combine the words picked up randomly from 2 word lists
var wordlist1 = ["option1", "option2", "option3"];
var wordlist2 = ["optionA", "optionB", "optionC"];

for (var i = 0; i < 10; i++){
var index1 = Math.floor(Math.random() * 3);
var index2 = Math.floor(Math.random() * 3);
var word0 = wordlist1[index1] + " " + wordlist2[index2];
// the process to the word that's generated randomly
}

Thank you, I actually already have the second piece and am not having issues combining words from the two wordlists, my issue is limiting potential combinations to 8 characters in length.
 
Thank you, I actually already have the second piece and am not having issues combining words from the two wordlists, my issue is limiting potential combinations to 8 characters in length.
Oh. I thought it was about coding. If you could be a little more specific about what you want to implement/mean by "limiting potential combination to 8 characters", I could get a hint.
For example,

  • You want to initialize the output textbox and show some prompt message when the length of combied words are longer than 8? or
  • You want the words in second list be grayed out when the option in first list was picked up and it makes it longer than 8? or
  • You'd rather exclude the combined words that have more than 8 characters before making a options list to show in the page? etc.

Typical method in things like this is to make a routine (function) to make an array to contain all combinations and check if there are any words that could be longer than 8 characters, then remove them from the options list.
Once it's done, you just simply make option list boxes in the page which don't have words that could make it longer than 8 when combined. Graying out method could be frustrating to the user.
 
Last edited:
Oh. I thought it was about coding. If you could be a little more specific about what you want to implement/mean by "limiting potential combination to 8 characters", I could get a hint.
For example,

  • You want to initialize the output textbox and show some prompt message when the length of combied words are longer than 8? or
  • You want the words in second list be grayed out when the option in first list was picked up and it makes it longer than 8? or
  • You'd rather exclude the combined words that have more than 8 characters before making a options list to show in the page? etc.

Typical method in things like this is to make a routine (function) to make an array to contain all combinations and check if there are any words that could be longer than 8 characters, then remove them from the options list.
Once it's done, you just simply make option list boxes in the page which don't have words that could make it longer than 8 when combined. Graying out method could be frustrating to the user.

Oh, I understand now. I guess I want to do the 3rd thing. I don't want any options with more than the character limit to show up at all.
Thank you for being patient with me :)
 
Oh, I understand now. I guess I want to do the 3rd thing. I don't want any options with more than the character limit to show up at all.
Thank you for being patient with me :)
No problem. :) And, ah I see. Actually there's a way that I thought about and didn't mention. This way could be more fun to the users, like, you just let them type anything they want in either of input boxes, and the program chooses the other piece randomly from the list that's prepared. But it requires regex to check inputs to make it work fine. It may be a bit too much for those who aren't very used to programming, I guess.
So. Here's a sample code to implement the third method. You can just put any words in those 2 arrays at the top part. Also, since I heard the max length is 10, I put 10 there, but you can change it to any length as well.


var maxi = Number(10);
var array01 = new Array('ABCDEFG', 'AB', 'ABC', 'ABCD', 'A', 'ABCDE');
var array02 = new Array('a', 'ab, 'abc', 'abcd', 'abcde');
var ar = checklen(array01, array02, maxi);
var ar2 = checklen(array02, array01, maxi);

for(var i=0; i<ar.length; i++){
 for (var j=0; j<ar2.length; j++){
  var w = ar + " " + ar2[j];
  document.write(w + " Length:" + w.length + "<p>");
}}

function checklen(array1, array2, max){
 for (var i=0; i<array1.length; i++){
  for (var j=0; j<array2.length; j++){
   var word = array1 + " " + array2[j];
   if (word.length > max){
    array1.splice(i, 1);
    if (i>0){
     i--;
    }
   }
  }
 }
 return array1;
}
 
Back
Top