3

I want to generate a random integer. When I enter a number in a textbox and call the function by clicking on a button, it says "The number was undefined", instead of giving the value of the generated number. Here is my code:

function getRandomInt(min, max){
    return Math.floor(Math.random(max-min+1)+min);
}

var array=[];
var integer=getRandomInt(1,100);
var broj=array[integer];
1
  • 4
    In your current code, array is empty, so array[integer] will always be undefined, no matter the value returned by getRandomInt Commented Nov 18, 2019 at 13:59

3 Answers 3

1

function getRandomInt(min, max) {
  return Math.random() * (max - min) + min;
}
var array=[];
function genRateNumber(){

var integer=Math.floor(getRandomInt(1,100));
array.push(integer)
console.log('array :'+array)
console.log('current Genrated  Value:'+integer)
}
<button onclick="genRateNumber()">genrate number</button>

Sign up to request clarification or add additional context in comments.

Comments

0

Your array is empty. You need to push your generated value:

function getRandomInt(min, max){
    return Math.floor(Math.random(max-min+1)+min);
}

let array=[]; // here array is empty
let integer=getRandomInt(1,100);
let broj=array.push(integer);
console.log(integer);

Comments

0

function getRandomInt(min, max){
    return Math.floor(Math.random() * (max - min)) + min;
}
var integer1=getRandomInt(1,100);
console.log(integer1);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.