I'm new to javascript and I'm trying to make random numbers every time the user clicks a button. But now the numbers are added to the end of the array and therefor more numbers appear instead of a total reset.
Can somebody help me ?
Here is what I tried so far :
let randomNumbersArray = [];
const containerEl = document.querySelector(".container");
let numbersEl = document.querySelector(".numbers")
const randomNumberBtn = document.querySelector("#random-number-btn");
const showRandomNumbersEl = document.querySelector(".showRandomNumbers");
function getRandomNumbers(numbers, amount) {
for (let i = 0; i < numbers; i ) {
let randomNumber = Math.floor(Math.random() * amount);
randomNumbersArray.push(randomNumber);
}
}
function showRandomNumbers() {
for (let y = 0; y < randomNumbersArray.length; y ) {
showRandomNumbersEl.innerHTML = `<div >${randomNumbersArray[y]}</div>`
}
}
randomNumberBtn.addEventListener("click", (e) => {
getRandomNumbers(10, 1000);
showRandomNumbers();
})
@import url('https://fonts.googleapis.com/css2?family=Roboto:wght@300;400;700&display=swap');
* {
margin: 0;
padding: 0;
box-sizing: border-box;
}
body {
font-family: 'Roboto', sans-serif;
}
.showRandomNumbers {
display: grid;
grid-template-columns: repeat(8, 42px);
text-align: center;
gap: 5px;
margin-top: 10px;
}
.showRandomNumbers div {
border: 1px solid #000;
padding: 6px;
}
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
<link rel="stylesheet" href="style.css">
<script defer src="script.js"></script>
</head>
<body>
<div >
<div >
<h4>How many random numbers?</h4>
<input type="text" id="number-random-numbers">
<button id="random-number-btn">Get numbers</button>
</div>
<div >
</div>
</div>
</body>
</html>
CodePudding user response:
i came up with this (read comments):
function getRandomNumbers(numbers, amount) {
// basically your creating a new array each time you call the function
randomNumbersArray = Array(numbers).fill(undefined).map(_ => Math.floor(Math.random() * amount))
}
function showRandomNumbers() {
// here i reset the content of the container each time you call the function so every time it just shows the new ones
showRandomNumbersEl.innerHTML = '';
for (const rndNumber of randomNumbersArray) {
showRandomNumbersEl.innerHTML = `<div >${rndNumber}</div>`
}
}
CodePudding user response:
Obseravtions/Suggestions :
- Empty
randomNumbersArray
array before pushing new set of random numbers. showRandomNumbersEl.innerHTML
should be empty before showing the new set of records inshowRandomNumbers()
method
Demo :
let randomNumbersArray = [];
const containerEl = document.querySelector(".container");
let numbersEl = document.querySelector(".numbers")
const randomNumberBtn = document.querySelector("#random-number-btn");
const showRandomNumbersEl = document.querySelector(".showRandomNumbers");
function getRandomNumbers(numbers, amount) {
randomNumbersArray = [];
for (let i = 0; i < numbers; i ) {
let randomNumber = Math.floor(Math.random() * amount);
randomNumbersArray.push(randomNumber);
}
}
function showRandomNumbers() {
showRandomNumbersEl.innerHTML = '';
for (let y = 0; y < randomNumbersArray.length; y ) {
showRandomNumbersEl.innerHTML = `<div >${randomNumbersArray[y]}</div>`
}
}
randomNumberBtn.addEventListener("click", (e) => {
getRandomNumbers(10, 1000);
showRandomNumbers();
})
@import url('https://fonts.googleapis.com/css2?family=Roboto:wght@300;400;700&display=swap');
* {
margin: 0;
padding: 0;
box-sizing: border-box;
}
body {
font-family: 'Roboto', sans-serif;
}
.showRandomNumbers {
display: grid;
grid-template-columns: repeat(8, 42px);
text-align: center;
gap: 5px;
margin-top: 10px;
}
.showRandomNumbers div {
border: 1px solid #000;
padding: 6px;
}
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
<link rel="stylesheet" href="style.css">
<script defer src="script.js"></script>
</head>
<body>
<div >
<div >
<h4>How many random numbers?</h4>
<input type="text" id="number-random-numbers">
<button id="random-number-btn">Get numbers</button>
</div>
<div >
</div>
</div>
</body>
</html>