mardi 27 avril 2021

I have tried to make a code that on getting input of 2 numbers from the user, generates a random number

This is the code I have. The main.js file is currently empty I keep it there just for show. The styles.css is currently only making the font cursive, so again, I didn't mention it. I hope it can be fixed without major changes. I need it to generate a random number between the two input numbers from a user on the webpage. I expect it to give me a random number between for example 1 and 10. It should give me something like 3 or maybe 7. I ran the snippet and it didn't give me any clear error. It said that there is a code error in line 0 file "".

<!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 href="styles.css" rel="stylesheet">
    <script src="main.js" defer></script>
</head>
<body>
    <input type="text" placeholder="First Value" id="firstNum"><br>
    <input type="text" placeholder="Second Value" id="secondNum"><br>
    <input type="text" placeholder="Generated Number" id="genNum"><br>
    <button onclick="genRand();">Generate</button><br>
    

    <script>
        var num1 = document.getElementById("firstNum").value;
        var num2 = document.getElementById("secondNum").value;
        var numGen;

        function genRand(num1, num2) {
            Math.floor(Math.random() * (num2 - num1 + 0) + num1) = numGen;
            document.getElementById("genNum").value = numGen;
        }
        
    </script>
</body>
</html>



Aucun commentaire:

Enregistrer un commentaire