ArnisR Posted July 29, 2012 Report Posted July 29, 2012 Sveiki! Nepieciešams izveidot JavaScript funkciju, kas atrgriež divus nejauši izvēlētus skaitļus x un y, pie tam, šis skaitļu pāris nedrīkst sakrist ar izņēmumu masīvā esošajiem skaitļu pāriem. Tas izskatās apmēram šādi: Masīvs ar izņēmuma vērtībām: var myArray = [{x:3,y:8},{x:4,y:8},{x:5,y:8},{x:6,y:8},{x:7,y:8}]; Nejaušo vērtību iegūšana: var position = randomPosition(); Vienreiz izsaucot šo funkciju, tiek iegūtas nejaušas koordinātas - position["x"] un position["y"] Paldies! Quote
gurkjis Posted July 29, 2012 Report Posted July 29, 2012 http://lmgtfy.com/?q=javascript+random Quote
ArnisR Posted July 30, 2012 Author Report Posted July 30, 2012 ja nepareizi sapratu tad... nepareizi sapratu... <div>[{x:3,y:8},{x:4,y:8},{x:5,y:8},{x:6,y:8},{x:7,y:8}];</div> <div id="posistion"></div> <script> var myArray = [{x:3,y:8},{x:4,y:8},{x:5,y:8},{x:6,y:8},{x:7,y:8}]; var position = randomPosition(); function randomPosition(){ var var1 = Math.floor((Math.random()*10)+1); var var2 = Math.floor((Math.random()*10)+1); var length = myArray.length; for(var i = 0; i < length; i++) { if(myArray[i]['x'] == var1 && myArray[i]['y'] == var2){ return randomPosition(); } } return '{x:'+var1+',y:'+var2+'}'; } document.getElementById('posistion').innerHTML = position; </script> Paldies liels! :) Quote
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.