HacktoberFest: Week 3
Tags: javascript, webdev, react, github
Published: 2022-10-18 21:22:47

- [Fourth Issue](https://github.com/JesseRWeigel/battlemath/issues/95)
- [Pull Request](https://github.com/JesseRWeigel/battlemath/pull/96#event-7602301817)

---

>Problem

When you first run the code, you'll already notice a huge problem regarding the game.

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/jw9zeiduaufqu0osrtwd.png)

As seen in the image above, the question starts from `0[operator]0`, this might not be a problem with subtraction, addition, and multiplication, but what about division? What if the user wants to practice how to do a simple division? Well now there's a problem. Unlike, the other three operators, a `0/0` is not valid, The users knows that, but the game still forces them to write in the answer, which unsurprisingly will always be wrong.
<br />
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/2r8we3iyht4l2t664vpz.png)

Another issue that I find is when the user restarts a game, they are always sent to do an addition, which in my opinion is counter intuitive to the purpose of a "restart". 

Lastly, one thing I have noticed, is that questions regarding on how many times you change the mode will stay the same. This is the reason why `0/0` is possible, because the first value will always start from addition, and `0+0` will not result in an invalid answer.
<br />
>Coding

I noticed when reading the the code, that the owner seems to be using a function called `randomNumberGenerator` in a repetition. This is a custom function that they made specifically for creating a random number. It its basis, it's a good way to ensure that there are no repetition within the code, and make the entire program run more efficiently. However, there's a problem when the function that is created, results in an even more repetition. What I have noticed is that for each operator, `randomGenerator` is used, but in a slightly altered way. For example, subtraction will have its **subtrahend** be always lower than its **minuend** to ensure that they never go below zero. Addition and multiplication will always be fine regardless of what value is given as long as it's not decimals.

What I have came up as a solution is to create a new function called `randomNumber`. This function will call the `randomNumberGenerator` twice, and use change their values depending on what **operator** is used, and return an array that contains two values that will be used for each calculations.

While writing the function, I noticed that I can use this to ensure that when restarting, I can keep the two numerical values from being zero while also ensuring the previous mode will never revert back to a `+`. It also works the same when changing each modes, now instead of each mode keeping the same value from the start, they will always change depending on what mode they are currently in.

The use an array as a return value fixes all the problems regarding changing the modes, restarting and overall quality of the code.

> Demo

###### Idle Mode

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/a07fedq3461c653v23bs.png)

###### Changing Mode

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/caoiiix1smqj2obmuxn4.png)

###### Restarting

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/zutz3dt5t6jghicgyb4v.png)

Restarting now stays at its current mode while also ensuring that the value is valid for the given mode.
<br />
>Difficulties

The difficulties I had is understanding the very basis of a `return`. It was always engraved into my mind that return can only return a singular value all the time. I then remember from a few semesters back that you can actually return multiple values as long as they are of similar type using an array. This is when I came into the conclusion that an array is the best choice I have into fixing the repository's problem.