Twe*_*ath 13 javascript arrays jquery
我想过滤掉老虎机的模式,在这种情况下,如果它们具有相同的值,我希望选择以下索引.
如果应该输出索引0,2和6具有相同的值.我在想像函数调用这样的东西
if (win_filter([0, 2, 6] == "slot-2") {
console.log("You won");
}
Run Code Online (Sandbox Code Playgroud)
我的代码如下.
var final_score = new Array();
$(".shoot").click(function() {
//var numbers_array = ["slot-1", "slot-1", "slot-1", "slot-1", "slot-1", "slot-2", "slot-2", "slot-2", "slot-2", "slot-3", "slot-3", "slot-3", "slot-4", "slot-4", "slot-5"];
var numbers_array = ["slot-1", "slot-2", "slot-3", "slot-4", "slot-5"];
var target = $("div.window table");
target.find("tr td > div").each(function() {
$(this).fadeOut(function() {
$(this).removeAttr('class');
$(this).addClass(numbers_array[Math.floor(Math.random() * numbers_array.length)]);
$(this).fadeIn();
final_score.push($(this).attr("class"));
});
});
function filterResults(arr) {
return final_score.filter(function(el) {
return arr.some(function(e) {
return el.timeframe == e;
});
});
}
var result = filterResults(['0','2','6']);
console.log(result);
$.each(numbers_array, function(index, value) {
if (result == value) {
$(".notice").html("You have won.").addClass("success").show();
console.log("You won!");
}
});
console.log(final_score);
});
Run Code Online (Sandbox Code Playgroud)
编辑
如果不清楚我的意思是数组中的索引,如果我从这个生成的数组中选择索引0,2和6,那么它们的值将是(即使它们不相同).
0 => "slot-2", 2 => "slot-5", 6 => "slot-1"
Run Code Online (Sandbox Code Playgroud)
目标是检查所选索引是否具有相同的值输出.并且索引的数量不应该是硬编码的,它可以是从3个索引搜索到5个索引搜索的任何内容.
Array[0]
0 : "slot-2"
1 : "slot-3"
2 : "slot-5"
3 : "slot-5"
4 : "slot-4"
5 : "slot-3"
6 : "slot-1"
7 : "slot-4"
8 : "slot-1"
9 : "slot-2"
10 : "slot-2"
11 : "slot-4"
12 : "slot-5"
13 : "slot-1"
14 : "slot-4"
Run Code Online (Sandbox Code Playgroud)
首先,我避免对您的代码进行任何重大修改,因此在 jsfiddle 中,我将验证准确地编写在您期望的位置(在 内部on('click'))。
我做了什么:
在计算任何内容之前,我们需要槽数据。您已经将其保存在内部final_score,但执行此操作的函数是回调。等待回调值得吗?- 我不这么认为,因为它是一个简单的css( fadeOut)动画。
const classVal = numbers_array[Math.floor(Math.random() * numbers_array.length)];
$(this.fadeOut(function() { // Rest of your code }
final_score.push(classVal);
Run Code Online (Sandbox Code Playgroud)
计算每一行的长度,您知道它们的长度都相等,并且它们都位于一个数组 ( final_score) 中,因此简单除以行数就足够了。
const lines = 3;
const lineLength = final_score.length/lines;
Run Code Online (Sandbox Code Playgroud)
对于每一行,我们检查其他行的值是否与这一行相同。鉴于您的数组顺序不是基于显示顺序,而是基于生成它们的顺序,我们可以简单地检查具有相同索引的数组(但迭代每一行)。
final_score[i] === final_score[i+lineLength] && final_score[i] === final_score[i+lineLength*2]
导致:
const lineLength = final_score.length/3;
for(let i=0; i<lineLength; i++) {
if (final_score[i] === final_score[i+lineLength] && final_score[i] === final_score[i+lineLength*2]) {
console.info(`win col ${i}`);
}
}
Run Code Online (Sandbox Code Playgroud)
如果你需要它,你可以轻松做到n-ify这一点。
const lineLength = final_score.length/3;
for(let i=0; i<lineLength; i++) {
const lineOneSlot = final_score[i];
let allEqual = true;
for (let j=1; j<lines; j++) {
console.info(`Comparing ${i} ${i+lineLength*j}`);
if (lineOneSlot !== final_score[i+lineLength*j]) {
allEqual = false;
break;
}
}
if (allEqual) {
console.info(`win col ${i}`);
}
}
Run Code Online (Sandbox Code Playgroud)
由于您还要求进行对角线检查,因此它看起来像这样:
但是,您必须确保网格是正方形才能获得预期结果。否则,您将不得不重新定义在这些情况下到底要做什么。
final_score[i] === final_score[i+1+lineLength] && final_score[i] === final_score[i+line+lineLength*line]
https://jsfiddle.net/qjp7g0qL/3/