发表于: 2019-07-25 13:33:06

1 918


完成 :  js 任务一

遇到的困难:  重复选中两个盒子 , 颜色 随机 


function co(){
this.r = Math.floor(Math.random() * 255);
   this.g = Math.floor(Math.random() * 255);
   this.b = Math.floor(Math.random() * 255);
   return "rgb" + "\(" + this.r + "," + this.g + "," + this.b + "\ )";
}

将随机  乘于颜色的 最大值(255),各自指向 r,g,b三个变量 (得出的结果不一样)、


重复盒子 解决 :


在 全局创建一个 a 的数组

for (var j = 0; j <= 8; j++) {
li_lis[j].style.backgroundColor = "#FCC03C";
   a.push(j)

li_lis 是9个盒子

每一次 点击闪     先进行9个盒子的遍历 把全部盒子的颜色返回初始颜色

同时  把 9个盒子的索引赋给 为 a的数组

function pq() {
var ge = Math.round(Math.random() * 8);

   if(a[ge] === undefined){
pq()
} else  {
li_lis[ge].style.backgroundColor = co();
       delete a[ge]
}
}


var ge = Math.round(Math.random() * 8);

实现随机 数 (索引—— ge) 

if(a[ge] === undefined){
pq()
else  {
li_lis[ge].style.backgroundColor co();
   delete a[ge]
}

每次进来 判断 a的索引(ge)的值  是不是undefined

如果不是 ,  把  li_lis (ge)染颜色 , 顺便 删除 a【ge】 的值 【delete  删除 数组索引的值

 下次进来 要是 还是上一次的值(例:3),那么  a【3】的值 是undefined  启用回调函数

重新 随机 ,  直到 抽选的值不一样 。 


for (var i = 0; i < 3; i++) {
pq();
   }
//运行结束过后 , 有3个值会是 undefined ,把数组重新清0 【因为上面每次都会重新添加数组
   a.length = 0
};

循环3次后, 得到结果 ,清理 把 数组a 清理为0 





返回列表 返回列表
评论

    分享到