Hi!
My name is Jon, I live in Sweden and I’m 41 years old, this is my first post, and my first program.
So I finally decided to learn coding and decided to go with JavaScript. I have spent two days studying the syntax, looking at some code and following a few small tutorials. Today I decided to write my first program from scratch. It’s a simple program that rolls two dices, adds them up, and displays the results in numbers and a graph. I also made a scale function, so the graph looks ok wether you roll the dices 50 times or 10.000 times.
All I wish for is someone to look at my code and let me know if I’m on the right path, or if my code is bad and why so. It seems important to start thinking in “the right way” from scratch, since there is so many ways to write code. I don’t want to develop any bad habits.
So here it goes:
<html>
<canvas id="myCanvas" height="400" width="600" style="border:3px solid #888888;"></canvas>
<script>
let canvas = document.getElementById('myCanvas');
let ctx = canvas.getContext('2d');
let i = 0; // initialize counter
let numberOfThrows = 600;
let scale = 800 / numberOfThrows;
let sumOfDices;
let graphArray = [0,0,0,0,0,0,0,0,0,0,0,0,0];
let darkCol = '#666666';
let brightCol = '#AAAAAA';
let fps = 50; // frames per second = speed
mainFunction();
function mainFunction() {
if (i < numberOfThrows) {
setTimeout(function() {
dice1 = randomDice();
dice2 = randomDice();
sumOfDices = dice1 + dice2;
graphArray[sumOfDices] = ++graphArray[sumOfDices];
drawEverything();
mainFunction();
}, 1000/fps);
i++;
}
};
function randomDice(){
dice = Math.floor(Math.random() * 6);
return dice + 1;
}
function drawEverything(){
// clears the canvas:
drawRect(0, 0, canvas.width, canvas.height, '#EEEEEE');
// draws the boundary lines:
drawRect(100, 120, 3, 270, brightCol);
drawRect(10, 110, canvas.width-20, 3, brightCol);
drawTitle();
drawGraph();
drawSidebar();
}
function drawTitle(){
drawText('Dices', 20, 50, '30px Arial', darkCol);
drawText('Probability of the sum of 2 dices', 20, 70, '14px Arial', brightCol);
}
function drawSidebar(){
drawText('Throws:', 20, 150, '14px Arial', darkCol)
drawText(numberOfThrows, 20, 177,'30px Arial', brightCol);
drawText('Throw:', 20, 200, '14px Arial', darkCol)
drawText(i, 20, 227,'30px Arial', brightCol);
drawText('Dice 1:', 20, 250, '14px Arial', darkCol)
drawText(dice1, 20, 277,'30px Arial', brightCol);
drawText('Dice 2:', 20, 300, '14px Arial', darkCol)
drawText(dice2, 20, 327,'30px Arial', brightCol);
drawText('Sum:', 20, 350, '14px Arial', darkCol)
drawText(dice1 + dice2, 20, 377,'30px Arial', brightCol);
}
function drawGraph(){
drawText('Counts:', 120, 350, '14px Arial', brightCol);
drawText('Sum:', 120, 380, '14px Arial', darkCol);
for (count=0; count < 12; count++){
drawText(graphArray[count+1], 180+(count*35), 350,'14px Arial', brightCol);
drawText(count+1, 180+(count*35), 380,'14px Arial', darkCol);
// draws the diagram:
drawRect(180+(count*35), 320-graphArray[count+1]*scale, 20, graphArray[count+1]*scale, darkCol)
}
}
function drawRect(x, y, width, height, color){
ctx.fillStyle = color;
ctx.fillRect(x, y, width, height);
}
function drawText(text, x, y, font, color){
ctx.font = (font);
ctx.fillStyle = color;
ctx.fillText(text, x, y);
}
</script>
</html>