samedi 5 septembre 2020

Not generating random number

I am making a website that will generate a random number using JS. But it shows undefined for some reason. I DON'T want that to come. Here is my code.

var showRandomNumberDiv = document.getElementById("show-random-number");
var number1 = document.getElementById("number-1");
var number2 = document.getElementById("number-2");
var generate = document.getElementById("generate");

var newPara = document.createElement("p");
generate.addEventListener("click", function() {
  numbers = [];
  for (var i = 0; i > number1 && i < number2; i++) {
    numbers.push(i);
  }
  newPara.innerHTML = numbers[Math.floor(Math.random() * numbers.length)];
  showRandomNumberDiv.appendChild(newPara);
})
<h1>Hello, and welcome to Random generator!</h1>
<h2>Not only numbers, but letters too!</h2>
<p>If you want to enter a <span style="font-style:italic; font-weight: bold;">positive</span> number, see below.</p>

<!-- Generate a random number -->

<input type="text" placeholder="number 1" id="number-1">
<input type="text" placeholder="number 2" id="number-2">
<input type="button" value="Generate" id="generate">
<div id="show-random-number">

</div>

<script src="javascript.js"></script>



Aucun commentaire:

Enregistrer un commentaire