I think that I’m going to improve some things while working out what’s going on.
First, we can use a separate function to add a leading zero.
function twoDigits(num) {
if (num < 10) {
return "0" + String(num);
}
return String(num);
}
for (let i = 0; i < ranNum.length; i++) {
if (i < ranNum.length - 1) {
firstFive += twoDigits(ranNum[i]) + " " + " " + " " + " " + " " + " ";
} else {
pb = twoDigits(ranNum[i]);
}
fiveNum.innerHTML = firstFive;
lastNum.innerHTML = pb;
}
Then, we can use padStart to simplify the twoDigits function.
function twoDigits(num) {
return String(num).padStart(2, "0");
}
Next, the contents of the loop should be put into an array, so that we can join it together with the desired separators.
const numbers = [];
for (let i = 0; i < ranNum.length; i++) {
numbers.push(twoDigits(ranNum[i]));
}
fiveNum.innerHTML = numbers.join(" " + " " + " " + " " + " " + " ");
The lastNum part doesn’t seem to be needed anymore, so we can remove that. I’ll rename fiveNums to be generatedNumbers too.
<div style="background-color:black">
<span id="generatedNumbers" style="margin-left: 10%; color:blue;"></span>
</div>
// var fiveNum = document.getElementById('fiveNums');
var generatedNumbers = document.getElementById('generatedNumbers');
// var lastNum = document.getElementById('lastNum');
...
generatedNumbers.innerHTML = numbers.join(" " + " " + " " + " " + " " + " ");
The HTML code is full of inline styles. Those really need to be moved out to the CSS section instead.
.container {
margin-left: 20%;
width: 500px;
font-weight: bold;
font-size: 30px;
}
button {
margin-left: 36%;
}
label {
margin-left: 20%;
}
.numberArea {
background-color: black;
}
#generatedNumbers {
margin-left: 10%;
color: blue;
}
<div class="container">
<button id="generate">Generate Numbers</button>
<br><br>
<div>
<label>Generate Numbers</label>
<div class="numberArea">
<span id="generatedNumbers"></span>
</div>
</div>
</div>
I don’t like the double break. <br>
tags really should only be used for separating addresses and doing poetry. Better techniques exist outside of those two situations.
The break can be removed, and replaced with a bottom margin on the button instead,
button {
margin-left: 36%;
margin-bottom: 3em;
}
<div class="container">
<button id="generate">Generate Numbers</button>
<div>
There are a lot of margin-left declarations, designed to center things.
I’m going to remove all of those and attempt to simplify things, by using some classes to center the content.
<div class="container center-block">
<button id="generate" class="center-block">Generate Numbers</button>
<div>
<h1 class="center-text">Generate Numbers</h1>
<div class="numberArea center-text">
<span id="generatedNumbers"></span>
</div>
</div>
</div>
.container {
width: 500px;
padding: 0.5em;
}
h1 {
width: 100%;
margin-top: 1em;
margin-bottom: 0em;
}
.numberArea {
height: 1em;
background-color: black;
color: blue;
font-size: 30px;
font-weight: bold;
}
.center-block {
display: block;
margin: 0 auto;
}
.center-text {
text-align: center;
}
That’s a bit better.
I’ll carry on next now with improving the scripting code.