Search
 
SCRIPT & CODE EXAMPLE
 

JAVASCRIPT

get random number with min and max

const randomNumber = getRandomNumberBetween(1,1000)
const getRandomNumberBetween = (min, max) => {
    return Math.floor(Math.random() * (max - min + 1) + min)
}
Comment

generate a random number between min and max

var min = 1;
var max = 90;
var stop = 6;  //Number of numbers to extract

var numbers = [];

for (let i = 0; i < stop; i++) {
  var n =  Math.floor(Math.random() * max) + min;
  var check = numbers.includes(n);

if(check === false) {
  numbers.push(n);
} else {
  while(check === true){
    n = Math.floor(Math.random() * max) + min;
    check = numbers.includes(n);
      if(check === false){
        numbers.push(n);
      }
    }
  }
}

sort();

 //Sort the array in ascending order
 function sort() {
   numbers.sort(function(a, b){return a-b});
   document.getElementById("array_number").innerHTML = numbers.join(" - ");
}
Comment

random numbers function (min, max)

//math.random gives us numbers between 0 and 1
//when we multiply it on max-min we get numbers between 0 and (max-min)
//after that we add min both max and min so we have number between min and max
//function gives us random numbers between 2 numbers we will specify
const randomInt = (min, max) =>
  Math.floor(Math.random() * (max - min) + 1) + min;
randomInt(min, max);
Comment

PREVIOUS NEXT
Code Example
Javascript :: js replace space with dash 
Javascript :: how to use datepipe in ts file 
Javascript :: Javascript adding zeros to the beginning of a string 
Javascript :: javascript int to float 
Javascript :: mongoose nestjs installation 
Javascript :: sweet alert after click ok redirect 
Javascript :: node js create folder 
Javascript :: jquery is checkbox checked 
Javascript :: express req ip address 
Javascript :: install node js in manjaro 
Javascript :: javascript gcd 
Javascript :: node log without newline 
Javascript :: how to store objects in localstorage 
Javascript :: how to convert string to int js 
Javascript :: arraylist of characters 
Javascript :: regex to allow only numbers letters and hyphen 
Javascript :: create-react-app 
Javascript :: how to refresh slick after tab function 
Javascript :: moment format sql date 
Javascript :: jquery disable class attribute 
Javascript :: js change html lang 
Javascript :: data structures for coding interviews in javascript 
Javascript :: javascript get css variable 
Javascript :: chartjs how to disable animation 
Javascript :: delete with unlinksync node 
Javascript :: expo ap loading 
Javascript :: react aos 
Javascript :: python request text to dict 
Javascript :: Your global Angular CLI version (11.0.2) is greater than your local version 
Javascript :: message.channel.fetchMessages is not a function 
ADD CONTENT
Topic
Content
Source link
Name
1+8 =