Good day.

The random number generator does not work for me the way I wanted. Note the comments inside the JS code. The problem is that if on a line in var raund = Math.floor(Math.random() * (ot) + **вставить переменную ->** doo); code does not work. And if as I indicated in the example, it works, but not the proofreader. Just do not put in the example from 50 to 100, everything will work, and when 444, 1244 - no.

 $(".rezult").html("dfgdf"); $(function() { $("input[name='Go']").click(function() { var ot = $(".ot").val(); var doo = $(".do").val(); var rezutl = doo - ot; //Так работает //НУЖНО ЗАКОММЕНТИРОВАТЬ var raund = Math.floor(Math.random() * (ot) + rezutl); //Работает//НУЖНО ЗАКОММЕНТИРОВАТЬ //var raund = Math.floor(Math.random() * (ot) + doo); //А так нет $(".rezult").html("<h1>" + "Результат: " + raund + "</h1>"); }); }); 
 .ot { height: 100px; max-width: 180px; font-size: 57px; color: aquamarine; margin: 0 14px 0 0; } .do { height: 100px; max-width: 180px; font-size: 57px; color: #59aef1; } .button { margin: 20px 0px 0px 0px; height: 50px; width: 200px; background: #fff; color: cadetblue; font-size: 22px; cursor: pointer; border: 1px solid cadetblue; } 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.1.1/jquery.min.js"></script> <center> <div class="generator_chisel"> <h1>Генератор случайных чисел</h1> <div class="rezult"> </div> <form action="" name="activ"> <input type="number" name="ot" class="ot" placeholder="ОТ"> <input type="number" name="do" class="do" placeholder="ДО"><br> <input type="button" name="Go" value="Генерировать" class="button"> </form> </div> </center> 

    1 answer 1

    You do not get numbers from fields, but strings. When you take them away in advance, they are automatically converted, i.e. when subtracting, the strings are converted to numbers themselves. To convert strings manually, I usually use parseFloat() . But there are alternative functions: Number() , parseInt() .

     $(".rezult").html("dfgdf"); $(function() { $("input[name='Go']").click(function() { var valOt = parseFloat( $(".ot").val() ) || 0, valDo = parseFloat( $(".do").val() ) || 100; var min = valOt > valDo ? valDo : valOt, max = valOt > valDo ? valOt : valDo, raund = Math.floor(Math.random() * (max - min + 1)) + min; $(".rezult").html("<h1>" + "Результат: " + raund + "</h1>"); }); }); 
     .ot { height: 100px; max-width: 180px; font-size: 57px; color: aquamarine; margin: 0 14px 0 0; } .do { height: 100px; max-width: 180px; font-size: 57px; color: #59aef1; } .button { margin: 20px 0px 0px 0px; height: 50px; width: 200px; background: #fff; color: cadetblue; font-size: 22px; cursor: pointer; border: 1px solid cadetblue; } 
     <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.1.1/jquery.min.js"></script> <center> <div class="generator_chisel"> <h1>Генератор случайных чисел</h1> <div class="rezult"> </div> <form action="" name="activ"> <input type="number" name="ot" class="ot" placeholder="ОТ"> <input type="number" name="do" class="do" placeholder="ДО"><br> <input type="button" name="Go" value="Генерировать" class="button"> </form> </div> </center> 

    I improved your code a bit, adding cases to it if the user doesn’t enter anything, or "from" will be more than "to"