An warware: javascript bazuwar janareta launi

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.

Shafi posts:

Leave a Comment