JSFiddle - React, Tailwind, and code Playground
by heybubbles
HTML
<script src="https://code.jquery.com/jquery-3.6.0.min.js"></script>
<script src="https://code.jquery.com/jquery-latest.min.js"></script>
<script src="https://cdn.jsdelivr.net/mark.js/8.6.0/jquery.mark.min.js"></script>
<section>
<h1>Random numbers generator</h1>
<h4>Generate any number of random values with or without repeats</h4>
<div class="group">
<label for="minimum">Minimum:</label>
<input type="number" step="1" id="minimum"/>
</div>
<div class="group">
<label for="maximum">Maximum:</label>
<input type="number" step="1" id="maximum"/>
</div>
<div class="group">
<label for="resultcount">How many results?</label>
<input type="number" min="1" step="1" id="resultcount" placeholder="1"/>
</div>
<div class="group">
<label>Keyword 1</label>
<input type="text" name="keyword">
</div>
<div class="group">
<label>Keyword 2</label>
<input type="text" name="keyword2">
</div>
<div class="boxgroup">
<input type="checkbox" id="norepeat"/>
<label for="norepeat">No repeat results</label>
</div>
<div class="boxgroup">
<input type="checkbox" id="sort"/>
<label for="sort">Order results numerically</label>
</div>
<button onClick="crunch()">Generate</button>
<div class="context" id="result"></div>
<div class="boxgroup">
<!-- partial -->
<script src="./script.js"></script>
CSS
* {
box-sizing: border-box;
}
body {
background: #f6ffec;
margin: 0;
font-family: Sahitya;
font-size: calc(.8rem + 1vw);
color: #53565a;
}
body section {
display: flex;
justify-content: flex-start;
flex-wrap: wrap;
position: relative;
width: 100%;
max-width: 24em;
min-height: 100vh;
margin: auto;
padding: 1em;
align-items: center;
align-content: center;
}
body section h1 {
font-size: 1.8em;
margin: 0;
line-height: 0.9;
}
body section h4 {
font-size: 0.7em;
font-weight: normal;
margin: 0.5em 0 32px;
}
body section .group, body section .boxgroup {
display: flex;
justify-content: flex-start;
flex-wrap: wrap;
justify-content: space-between;
width: 100%;
flex-wrap: wrap;
margin-bottom: 1em;
}
body section .group label, body section .boxgroup label {
flex: 1 0 auto;
line-height: 2;
position: relative;
top: 0em;
border-bottom: 1px solid #53565a;
}
body section .group input, body section .boxgroup input {
flex: 10 0 auto;
padding: 0 0 0.1em 0.5em;
font-family: Sahitya;
font-size: 22px;
border: none;
border-bottom: 1px solid #53565a;
background: none;
margin: 0;
text-align: right;
}
body section .group input:focus, body section .boxgroup input:focus {
outline: none;
}
body section .boxgroup {
width: 100%;
justify-content: flex-start;
}
body section .boxgroup label {
text-align: left;
flex: 10 0 auto;
padding-left: 1.2em;
}
body section .boxgroup label:before {
content: "";
font-size: 120%;
line-height: 0.5em;
width: 0.6em;
text-indent: 0.075em;
height: 0.6em;
border: 1px solid #53565a;
position: absolute;
left: 0;
bottom: 0.5em;
}
body section .boxgroup label.checked {
font-weight: bold;
}
body section .boxgroup label.checked:before {
content: "x";
}
body section .boxgroup input {
width: auto;
flex: 1 0 auto;
margin: 0.32em 0.4em 0 0;
border-bottom: 1px solid black !important;
display: none;
}
body section button {
font-family: Sahitya;
...
JavaScript
var resultArea = document.querySelector('#result');
var noRepeatBox = document.querySelector('#norepeat');
var sortBox = document.querySelector('#sort');
var noRepeatLabel = document.querySelector('label[for=norepeat]')
var sortLabel = document.querySelector('label[for=sort]')
noRepeatBox.addEventListener('click', function(){
noRepeatLabel.classList.toggle('checked');
});
sortBox.addEventListener('click', function(){
sortLabel.classList.toggle('checked');
});
$(function() {
var mark = function() {
// Read the keyword
var keyword = $("input[name='keyword']").val();
var keyword2 = $("input[name='keyword2']").val();
// Remove previous marked elements and mark
// the new keyword inside the context
$(".context").unmark({
done: function() {
$(".context").mark(keyword).mark(keyword2, {
className: 'secondary'
});
}
});
};
$("input[name^='keyword']").on("input", mark);
});
function crunch(){
var noRepeat = document.querySelector('#norepeat').checked;
var sort = document.querySelector('#sort').checked;
// var min = 10;
var min = Number(document.querySelector('#minimum').value);
//console.log(min);
// var max = 99;
var max = Number(document.querySelector('#maximum').value);
//console.log(max);
var resultCount = Number(document.querySelector('#resultcount').value);
var resultsArray = [];
resultArea.innerHTML = '';
if((max - min) == 0) {
resultArea.innerHTML = '<span class="error">Please choose a valid range.</span>'
return;
} else if (max < min) {
resultArea.innerHTML = '<span class="error">Invalid number range</span>'
return;
} else if (noRepeat && resultCount > ((max - min) + 1)) {
resultArea.innerHTML = '<span class="error">Range is too small to generate only unique numbers</span>'
return;
}
if(!resultCount) {
resultCount = 1;
}
function newNumber() {
if (min == 0) {
var result = Math.floor(Math.random() * (max + 1));
} else if (min < 0 && max >= 0) {
var result =...