44 lines
1.5 KiB
JavaScript
44 lines
1.5 KiB
JavaScript
|
const randomSzamInput = document.getElementById("randomSzam")
|
||
|
const generaltSzamok = document.getElementById("generaltSzamok")
|
||
|
const button = document.querySelector("button")
|
||
|
|
||
|
button.addEventListener("click", () => {
|
||
|
const randomSzamok = []
|
||
|
let generaltSzamokChildren = Array.from(generaltSzamok.children)
|
||
|
generaltSzamokChildren.forEach(child => {
|
||
|
generaltSzamok.removeChild(child)
|
||
|
})
|
||
|
|
||
|
let myPromise = new Promise((resolve, reject) => {
|
||
|
let success
|
||
|
if (Number.isInteger(Number(randomSzamInput.value))) {
|
||
|
success = true
|
||
|
} else {
|
||
|
success = false
|
||
|
}
|
||
|
|
||
|
setTimeout(() => {
|
||
|
if (success) {
|
||
|
for (let i = 0; i < Number(randomSzamInput.value); i++) {
|
||
|
let randomSzam = Math.floor(Math.random() * 100)
|
||
|
randomSzamok.push(randomSzam)
|
||
|
}
|
||
|
resolve(randomSzamok)
|
||
|
} else {
|
||
|
reject("Probléma volt a kód futásával!")
|
||
|
}
|
||
|
}, 2000)
|
||
|
})
|
||
|
|
||
|
myPromise
|
||
|
.then(result => {
|
||
|
let generaltSzamokP = document.createElement("p")
|
||
|
generaltSzamokP.innerHTML = `Generált számok: ${result.join(", ")}`
|
||
|
generaltSzamok.appendChild(generaltSzamokP)
|
||
|
})
|
||
|
.catch(error => {
|
||
|
let generaltSzamokP = document.createElement("p")
|
||
|
generaltSzamokP.innerHTML = error
|
||
|
generaltSzamok.appendChild(generaltSzamokP)
|
||
|
})
|
||
|
})
|