Mixed Messages - Character Sheet Generator

Hello everyone!

I found this project to be fairly straightforward. I still want to do some refactoring. I might go through and combine all of the functions into a factory function so that the user can simply call the function as needed. I’d like to keep working on this as I get more comfortable with JS and maybe in the future integrate it into a bigger project. I think two big things it needs are to relate the class in the bio to the stats generated and some sort of evaluation of how the character would engage in combat (or avoid it).

I spent probably eight hours on this project so far.

Here’s a link to what I have so far:

FreakingLoki’s Character Sheet Generator

I would love to hear any feedback anyone would like to give. I may not respond quickly but I will make an effort to. Thank you all in advance.

1 Like

This is a really awesome project — the code is well written and the story elements are super fun!

One big technical issue :bangbang:

On lines 78 and 79, you get random numbers based on the length of the firstNames and lastNames arrays plus one. The result of this is that there is a chance the indices passed in to create charName on line 81 could be outside the bounds of the array. In the screenshot below, note that the last name is undefined:

Let’s look at why.

firstNames has a length of 40, so on line 78 we evaluate Math.floor(Math.random() * 41) since we add 1 to the length. Math.random() generates a random decimal value between 0 (inclusive) and 1 (exclusive). Multiplied by 41, we can expect a decimal value between 0 (inclusive) and 41 (exclusive), so 0-40.9999…

Math.floor() will then round the value returned by Math.random() down to the nearest integer. This makes the possible values of numOne on line 78 any integers between 0 and 40, both inclusive. Since the length of the array is 40, its valid indices are integers 0 through 39 — if numOne ends up being 40, it goes out of the bounds of the array and returns undefined!

Since only one index is out of range, you can resolve this by just using .length instead of .length + 1 on each affected line (78, 79, 123).

My only other suggestion would be to create template functions for generating random numbers. Already within this code, there are several instances of Math.floor(Math.random() * number). I imagine generating random numbers will continue to be a common occurrence as you work on this project.

You could cut out some of that repetitive typing (i.e. chances for typos) by writing a function like this:

const getRandomInt(multiplier) {
  const randomInt = Math.floor(Math.random() * multiplier);
  return randomInt;

Then, the call on line 5 could be rewritten as

let luck = getRandomInt(51);

You could also define a similar function to wrap the construction on line 19, Math.floor(((1.4 * strength) + luck)); since that is reused several times as well.

Keep up the great work, happy coding! :grin:

Thank you so much for the reply! I didn’t notice the issue with the names and bios arrays, thanks for catching that, i’ll add fixing that issue to my to do list. I think that cropped up because I wanted the skills in the other functions to have a max value of 50 so I had to go back and add one to their multipliers at some point.

I’ll definitely take your advice on the getRandomInt(multiplier) function too, I should have noticed how much I was typing the same chunk of code and tried to simplify it. It is a process learning to think like a programmer.

1 Like