mardi 1 mai 2018

A webpage, that can generate 20 random numbers between 0-100, and then display all odd numbers, and all numbers that are greater or equal to 50

I'm looking to create a webpage, that can generate 20 random numbers between 0-100, and then display all odd numbers, and all numbers that are greater or equal to 50. I know you need .grep() in jQuery but I'm struggling to make the random numbers link to my other buttons. Also how do you find odd numbers through the random numbers. If possible can they stay on the same page Any suggestion or explanations will be appreciated?

<html lang="en">

   <head>

      <meta charset="utf-8">

      <script src="jquery-3.3.1.js"></script>

      <script>
//display random numbers
         $(document).ready(function() {
               $("#btn").click(function(){
               for ( i = 0; i <=20; i++ ) {
                    var randNum = 0;
                    var randNum = Math.floor(Math.random() * 100);
                     $("#con").append(" "+randNum);

               }});

           });

           // numbers over fifty
         $(document).ready(function() {
               $("#btn2").click(function(){

           var arr = [ ]
         while(arr.length <10){
           var randomnumber = Math.floor(Math.random()*51) + 50;


           if(arr.indexOf(randomnumber) > -1) continue;
           arr[arr.length] = randomnumber;
         }    

         document.write(arr);


               });

           });





      </script>
      <style>
         div {
         color: blue;
         }
         p {
         color: green;
         margin: 0;
         }
         span {
         color: red;
         }
      </style>
   </head>
   <body>
      <button type = "button" id="btn">Generate Random Numbers</button>
      <br/>
      <div id="con"></div>
      <br/><br/><br/>
      <button type = "button" id="btn1" >Get Odd Numbers </button>
      <br/><br/><br/>
      <button type = "button" id="btn2" >Display Numbers Above 50 </button>
   </body>
</html>




Aucun commentaire:

Enregistrer un commentaire