3 回答
TA贡献1865条经验 获得超7个赞
/*Check whether array named jogo has the temp element inside it, if not add the temp element to jogo. */
if (jogo.indexOf(temp) == -1){
jogo.push(temp)
}
/*When you are inside this if else block the i's value must have been increased, now if
temp is not being inserted because it's already there in the array, decrement i so that the if else block again runs with the same I what it was earlier before increment.
*/
else {
i--;
}
TA贡献1821条经验 获得超4个赞
此代码循环遍历整个数组以检查随机数是否在数组内。但如果不是,它将输出 -1 表示当前随机数没有重复
if (jogo.indexOf(temp) == -1)
虽然此代码只是反转循环,因此它取消了 i++。
i--;
TA贡献1829条经验 获得超13个赞
如果 temp(随机数)不在 jogo 数组中(如果找不到值,indexOf 总是返回 -1)
if (jogo.indexOf(temp) == -1)
const arr = ["A","B","C"];
console.log(arr.indexOf("D"));
console.log(arr.indexOf("C"));
然后将其添加到数组中
jogo.push(temp)
否则重做循环,希望得到一个不在 jogo 数组中的数字(-- 会抵消 ++ )
i--;
let i = 5;
i--
i++
console.log(i);
添加回答
举报