0

I am currently working on creating random word portions for a custom name generator. Each value is merged with 1-2 others to create the custom name. The problem is, I want certain word portions to have random letters included.

I currently have an array setup with each random word portion and a function that links to this random letter creator. The problem is, when the array is first ran through it sets these letters to specific things. Thus, it makes the output identical (for those "custom" letters).

My code is below:

//Letters in alphabet in order to generate more random outputs.
var alphabet = ["A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K", "L", "M", "N", "O", "P", "Q", "R", "S", "T", "U", "V", "W", "X", "Y", "Z"];

//Selects a random letter and outputs a return value.
var randomLetter = function(letterCase) {

    var letterID = randomNumber(alphabet.length);
    console.log(letterID);

    if(letterCase === "C") {
        return alphabet[letterID];

    } else if(letterCase === "L") {
        return alphabet[letterID].toLowerCase();

    } else {
        console.log("randomLetter function failed to generate a random letter. No set letter case.");
    }
};

//Base names for town name generations. Called within a function.
var townNameStart = ["Ji", "Ri", "Li", "We", "Wete", "Lakeside Zi", "Za", "Mount "+randomLetter("C")+randomLetter("L")];

If I ran console.log(townNameStart[7] +" "+ townNameStart[7]); I want it to output TWO COMPLETELY DIFFERENT letter combinations. I do not want it to end like this upon output: i.imgur.com/YruxwPq.png . It needs to have a completely different letter each time (without reloading the script). How can the function in the array be called (to assign the random letter) each time it is called to prevent the same letter output?

Should I have the array set itself to its original state every time the function is called? Which work around would be best (redefine array, or something else)?

Still looking for help.

bdkopen
  • 494
  • 1
  • 6
  • 16
  • Why do you have one function doing two things? Have a function that generates a random number (and test that it works) and pass a call to it to a function that returns the letter of the alphabet at that index (and test that the letter function works) `getLetter(genRandom(26));` or whatever. Also you don't need two arrays, `String.prototype.toUpperCase` exists. – Jared Smith May 07 '17 at 00:47
  • You can't figure out why your code doesn't work and your asking me how breaking the task into separate, minimal, individually testable parts is going to help? – Jared Smith May 07 '17 at 00:50
  • @JaredSmith I was getting errors using .toUpperCase, I'll look into it again to double check it wasn't on my part. Doing some changes. Is my question clear, or do I need to clarify? – bdkopen May 07 '17 at 00:58
  • Are you initializing `townNameStart` once and then using it multiple times? The obvious solution is to have a function which generates the equivalent of `townNameStart` every time you needed one. – Andrew Shepherd May 07 '17 at 01:00
  • Yes, townNameStart is initiated one time and then called upon with a function. Inside that function there is a loop referencing back multiple times (default at 10). – bdkopen May 07 '17 at 01:04

1 Answers1

0

You had some errors in the console. Always check it before posting here, and if there's an error post that as well.

//Letters in alphabet in order to generate more random outputs.
var alphabet = ["A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K", "L", "M", "N", "O", "P", "Q", "R", "S", "T", "U", "V", "W", "X", "Y", "Z"];

function randomNumber(len) {
  return Math.round(Math.random() * len)
}
//Selects a random letter and outputs a return value.
var randomLetter = function(lower) {
  var letterID = randomNumber(alphabet.length);
  if (lower) {
    return alphabet[letterID].toLowerCase();
  }
  return alphabet[letterID];
};

//Base names for town name generations.
var townNameStart = [
  "Ji", 
  "Ri", 
  "Li", 
  "We", 
  "Wete", 
  "Lakeside Zi", 
  "Za", 
  "Mount " + randomLetter() + randomLetter(true),
  "Saint " + randomLetter() + randomLetter(true)
];

console.log(townNameStart.join(', '))

Outputs Ji, Ri, Li, We, Wete, Lakeside Zi, Za, Mount Ua, Saint Bn

Can you clarify on

What would be the most efficient way to make the random letter different each time the array is called?

One option is to shuffle the array, but randomness is a challenge in and of itself.

// From http://stackoverflow.com/questions/6274339/how-can-i-shuffle-an-array
function shuffle(a) {
  for (let i = a.length; i; i--) {
    let j = Math.floor(Math.random() * i);
    [a[i - 1], a[j]] = [a[j], a[i - 1]];
  }
}

//Selects a random letter and outputs a return value.
var randomLetter = function(lower) {
  var letterID = randomNumber(alphabet.length);
    if (lower) {
    return alphabet[letterID].toLowerCase();
  }
  shuffle(alphabet);
  return alphabet[letterID];
};

Edit to address comments:

The error with the code copy-pasted directly to Plnkr:

(index):51 Uncaught ReferenceError: randomNumber is not defined
    at randomLetter ((index):51)
    at window.onload ((index):66)
randomLetter @ (index):51
window.onload @ (index):66

Also, can you explain how & why the object retains the function vs. the array?

This question is unclear.

Phix
  • 9,364
  • 4
  • 35
  • 62
  • Missread & tested your script, its late. If I attempt to call [7] in the array twice it will output the same value. I want each call to output a different word. Thus, making each output different instead of the same. If I ran `console.log(townNameStart[7] +" "+ townNameStart[7]);` with your code it'd output this http://i.imgur.com/YruxwPq.png instead of two words each with different random chars. Updating main post in hopes to clarify. Apologies. – bdkopen May 07 '17 at 01:52
  • No worries! I'll take a look later. – Phix May 07 '17 at 03:03
  • You are accessing the 7th index of the array `townNameStart`. Of course it's going to be the same. – Phix May 08 '17 at 22:32
  • Right, is there away I can set it up where it won't be the same. What would be the best way. I'd like to keep it in the simple array format but with the function putting a letter in for every call. Is there any possible way, and if not what would be the best alternative. – bdkopen May 09 '17 at 00:08
  • Access it with a random number. – Phix May 09 '17 at 00:14
  • Are you suggesting accessing the array with a random number? Thats what I do in the program. The example I posted above is just showing that if the same thing is called multiple times, it outputs the same thing. I want that specific array element to output those custom letters differently every time that specific element is called. If called twice in a row, the added letters need to be different, not the same as the program does now. – bdkopen May 09 '17 at 00:21
  • Which is where the array shuffle function comes in. I think you have all the pieces to complete this. Good luck. – Phix May 09 '17 at 01:43