Aviway Posted July 12, 2009 Share Posted July 12, 2009 If i use the function - "Math.random" with the "loop - for"(30 times), Can i "sum" all the result of the "Math.random" together???? <script language="JavaScript"> <!-- var x=33; var y=45; var str=""; for (loop=1;loop<30;loop++ ) {tempature=Math.round(Math.random()* (y - x) + x); str+=tempature+"\n"; } sum=str+tempature; alert(sum) //--> </script> Link to comment Share on other sites More sharing options...
Ingolme Posted July 12, 2009 Share Posted July 12, 2009 total = 0;for (loop=1;loop<30;loop++ ) temperature=Math.round(Math.random()* (y - x) + x); total += temperature}alert(total) Link to comment Share on other sites More sharing options...
Aviway Posted July 12, 2009 Author Share Posted July 12, 2009 TANX.and if i want to define for each time that the "if" function is true a "string" var total2000 = 0; var total2001 = 0; var avg00=""; var avg01=""; var str=""; for (loop=1;loop<30;loop++ ) {temperature=Math.round(Math.random()* (45 - 33) + 33); total2000 += temperature; avg00=Math.round(total2000)/30; } for (loop=1;loop<30;loop++ ) {temperature=Math.round(Math.random()* (45 - 33) + 33); total2001 += temperature; avg01=Math.round(total2001)/30; if (temperature>avg00) {str+=temperature+"\n"; } } alert("July 2000 AVG tempature is: "+avg00+"\n"+"July 2001 AVG tempature is: "+avg01+"\n"+str); that the alert will be like that:day "n" = 43 Link to comment Share on other sites More sharing options...
justsomeguy Posted July 13, 2009 Share Posted July 13, 2009 and if i want to define for each time that the "if" function is true a "string"huh?BTW, that loop is going to run 29 times, not 30. It will run when loop = 1..29. Link to comment Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.