ashri Posted July 2, 2010 Share Posted July 2, 2010 i have some problem with my script. Here is my script<script language="javascript">myarray = new Array();myarray[0] = "this is my first javascript"myarray[1] = "this is my second javascript"myarray[2] = "this is my third javascript"myarray[3] = "this is my forth javascript"myarray[4] = "this is my fifth javascript"myarray[5] = "this is my sixth javascript"myarray[6] = "this is my seventh javascript"myarray[7] = "this is my eighth javascript"myarray[8] = "this is my ninth javascript"myarray[9] = "this is my tenth javascript"i=0for(i=0;i<myarray.length;i++){alert(Math.floor(Math.random(myarray)*10))}</script>actually i want to display array text as an alert but alert box displaying array number please explain anyone what is wring with my script.thanks in advance Link to comment Share on other sites More sharing options...
dsonesuk Posted July 2, 2010 Share Posted July 2, 2010 you have to use myrarray.length to get a random number within the total number of items in the arrayran_array_number=Math.floor(Math.random(myarray.length)*10)then you use the number to display the item with in the array at that positionalert(myarray[ran_array_number]) Link to comment Share on other sites More sharing options...
thescientist Posted July 2, 2010 Share Posted July 2, 2010 also, you don't need to initialize i=0 twice. Doing it in the for loop is fine. Link to comment Share on other sites More sharing options...
ashri Posted July 3, 2010 Author Share Posted July 3, 2010 you have to use myrarray.length to get a random number within the total number of items in the arrayran_array_number=Math.floor(Math.random(myarray.length)*10)then you use the number to display the item with in the array at that positionalert(myarray[ran_array_number]) I have done as you mentioned and add this line in my scriptran_array_number=Math.floor(Math.random(myarray.length)*10)after that my script is looks like<script language="javascript">myarray = new Array();myarray[0] = "this is my first javascript"myarray[1] = "this is my second javascript"myarray[2] = "this is my third javascript"myarray[3] = "this is my forth javascript"myarray[4] = "this is my fifth javascript"myarray[5] = "this is my sixth javascript"myarray[6] = "this is my seventh javascript"myarray[7] = "this is my eighth javascript"myarray[8] = "this is my ninth javascript"myarray[9] = "this is my tenth javascript"i=0for(i=0;i<myarray.length;i++){ran_array_num=Math.floor(Math.random(myarray)*10)alert([ran_array_num])}</script>but the result is the same. alert displays only number of array not it's value I want to display array's value as an alert. so please help me in this way. Link to comment Share on other sites More sharing options...
ashri Posted July 3, 2010 Author Share Posted July 3, 2010 also, you don't need to initialize i=0 twice. Doing it in the for loop is fine. thanks for this suggestion buddy. Link to comment Share on other sites More sharing options...
Synook Posted July 3, 2010 Share Posted July 3, 2010 Your "modified" script and dsonesuk's don't look the same to me... Link to comment Share on other sites More sharing options...
ashri Posted July 3, 2010 Author Share Posted July 3, 2010 Your "modified" script and dsonesuk's don't look the same to me... script is diffrent but the result is same Link to comment Share on other sites More sharing options...
Synook Posted July 3, 2010 Share Posted July 3, 2010 You do not appear to have incorporated dsonesuk's suggestions into your code. He gave you this snippet: ran_array_number=Math.floor(Math.random(myarray.length)*10)alert(myarray[ran_array_number]) But your script still looks, in part, like this: ran_array_num=Math.floor(Math.random(myarray[i])*10)alert([ran_array_num]) Link to comment Share on other sites More sharing options...
ashri Posted July 3, 2010 Author Share Posted July 3, 2010 You do not appear to have incorporated dsonesuk's suggestions into your code. He gave you this snippet:ran_array_number=Math.floor(Math.random(myarray.length)*10)alert(myarray[ran_array_number]) But your script still looks, in part, like this: ran_array_num=Math.floor(Math.random(myarray[i])*10)alert([ran_array_num]) oh yes it have given me the result what i expected.thanks you very much and thnks to dsonesuk too. Link to comment Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.