Babban matsala tare da bazuwar janareta launi shine cewa ba da gaske ba ne. Idan an samar da launuka biyu daban-daban a lokaci guda, to za su kasance kusa da juna a launi.
var colors = ['#FF0000','#00FF00','#0000FF']; var rand = Math.floor(Math.random()*colors.length); document.bgColor = colors[rand];
Wannan layin lambar yana saita launi na bangon takarda zuwa launi bazuwar daga tsararru. Tsarin yana da launuka uku a ciki, ja, kore, da shudi. Layin lambar da farko ya ฦirฦiri wani maษalli da ake kira rand kuma ya saita shi daidai da lambar bazuwar tsakanin 0 da 2. Sannan ya saita launi na bangon takarda zuwa launi a cikin tsarar launuka a ma'aunin da yayi daidai da rand.
Aiki tare da launuka a JavaScript
Yin aiki tare da launuka a JavaScript na iya zama ษan wahala. Akwai 'yan hanyoyi daban-daban don yin shi, kuma kowanne yana da nasa amfani da rashin amfani.
Hanya ษaya don aiki tare da launuka a cikin JavaScript shine amfani da kayan launi na abubuwan da kuka ฦirฦira. Misali, zaku iya ฦirฦirar abu ja kamar haka:
var ja = {launi: 'ja'};
Hakanan zaka iya amfani da kayan launi don samun damar launin abun, kamar haka:
ja . launi; //' ja'
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, ko don ฦirฦirar gwaje-gwaje bazuwar.