alright as promised yesterday Iβm going to try making the Random Number Generator HTML again today without looking up anything or using any ChatGPT. I already solved that for Python yesterday, so now itβs just JS left
My Code
Tadaa! Turns out this time I was able to solve it fully on my own!
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>1-100 Random Generator Button</title>
</head>
<body>
<button id="js-random-button">Generate Random Number</button>
<div id="js-random-number"></div>
<script>
document.getElementById('js-random-button').onclick = function () {
let randomNumber = Math.floor(Math.random() * 100) + 1;
document.getElementById('js-random-number').textContent = randomNumber;
}
</script>
</body>
</html>
Next I want to get more reps with page manipulation.
I want to become so familiar with this that I can control website content with JavaScript without even thinking much, so Iβll add more exercises to achieve this in the coming days.
Top comments (2)
Way to go π. Small thing: if there is no element found, this code will fail. So itβs good practice to check that the element actually exists and handle that case π
Thanks! Mhh okay cool makes sense, will try to do that next time π