8th Week of #100DaysOfCode with JavaScript. What did I Learn so far?

Nick Hollins
3 min readJul 6, 2020

Topic I learned This Week: Practice/Project!

Unfortunately I had to skip a week of coding because I have been moving, but I am back on track and learning. I have not learned any new concepts, but I have been practicing what I have learned so far by creating a small project which is a dice game.

What have I incorporated in the project?

The first thing that I incorporated in this project is generating a random number for the dice when the player click the roll button. My thought process went like this: So the maximum number of a regular dice can be from 1 to 6. So my first google search was “how to generate a random number using JavaScript?”. I figured out that there is a method for this called Math.random() which generates a floating point from 0 to less than 1 inclusive of 0 but NOT 1. So I figured I can just code Math.random() * 6 and get random numbers between 1 and 6 right? Wrong! I quickly ran into a problem. The problem was that Math.random() * 6 generated a floating point from 0 up to but not including 6. Another problem was that I wanted the random numbers to be from 1 to 6 and I wanted these to be whole numbers not floating point numbers. Lucky for me these problems was only a google search away. So since I wanted the random numbers generated to be between 1 and 6, I found the Math.floor() method which basically returns the largest integer less than or equal to a given number. Knowing this I should have been able to add the Math.random() method to the Math.Floor() method in order to generate whole random numbers between 1 and up to and including 6 right? wrong! So once I entered the code : Math.floor(Math.random() * 6) in the browser console to test if it worked this is what I got:

As you can see on the last test I did, I got a a result of 0 but I wanted 1–6 not 0–5. No worries, just another google search away from figuring this out. I soon found out that all I had to do was add + 1. So once I entered the code: Math.floor(Math.random() * 6) + 1 I got the results that I was looking for. The only thing that was left for me to do was to take that whole piece of code and store it inside of a variable named dice so the outcome looked like this : var dice = Math.floor(Math.random() * 6) + 1;

What did I learn from this?

One of the biggest things I learned from this is the fact that the best way to solve a problem is to solve it piece by piece. What I am learning to do is break down a problem into the most smallest pieces that I can and work from there. This is better than trying to solve too many things at once, taking baby steps is perfectly fine. Another thing that I realized is that the more mistakes you make coding, the more you learn. This is another reason why you shouldn’t just follow tutorials and not try things on your own. In tutorials you don’t get the experience of making mistakes because you are just copying what you see and it works. Just understand you have to make mistakes in order to learn.

Follow me on social media

Facebook

Instagram

LinkedIn

--

--

Nick Hollins

Developer. Writer. Creator. Always learning. Feel free to email me: hollinsaquile@gmail.com