简体   繁体   中英

How to store 3 random numbers in a single dimensional array and then get alerted in Javascript

function next_question()
{


for (i = 0; i <= 2 ; i++)
{
    a = Math.floor(Math.random() * (8 - 0)) + 0;
ques_number[i] = a;

}

alert (ques_number[0]);


}

I want to create an Array, that has 3 random numbers in it and then get an alert of which numbers were stored in an Array! It seems simple but I'm doing something basically wrong here that my above code isn't working! Please help thanks

You need

  • a global variable ques_number ,
  • two local variables i and a ,
  • a simplified random part

 var ques_number = []; function next_question() { var i, a; for (i = 0; i <= 2 ; i++) { a = Math.floor(Math.random() * 8); ques_number[i] = a; } alert(ques_number[0]); } next_question(); document.write('<pre>' + JSON.stringify(ques_number, 0, 4) + '</pre>'); 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM