Babban matsalar ɗaukar lamba tsakanin lambobi biyu shine cewa zai yi wahala a yanke shawarar wacce za a zaɓa.
var min = 1; var max = 10; var random = Math.floor(Math.random() * (max - min + 1)) + min;
Wannan layin lambar yana ƙirƙirar maɓalli mai suna "random" wanda zai adana lambar bazuwar tsakanin 1 da 10.
Laburare na lissafi
Babu takamaiman amsa ga wannan tambayar saboda ya dogara da takamaiman bukatun aikin da aka bayar. Koyaya, wasu nasihu na gaba ɗaya waɗanda zasu iya amfani sun haɗa da:
1. Yi la'akari da yin amfani da ɗakin karatu kamar Math.js ko Math.pow don sauƙaƙe ayyukan lissafi masu rikitarwa.
2. Yi amfani da aikin Math.random() don samar da bazuwar lambobi don dalilai na gwaji.
3. Yi amfani da ayyukan Math.floor() da Math.ceil() don kashe lambobi zuwa ƙimar lamba mafi kusa ko ƙimar gabaki ɗaya mafi kusa, bi da bi.
Aiki na aiki
Bazuwar aiki a JavaScript aiki ne da ke dawo da lambar bazuwar. Wannan na iya zama da amfani don ƙirƙirar lambobi na musamman, misali lokacin da kuke ƙirƙira wasa ko simulation.