javascript - 如何在JavaScript中垂直比较数组中的字符串

javascript - 如何在JavaScript中垂直比较数组中的字符串,第1张

我正在尝试解决一个问题陈述,我需要垂直比较数组中的字符串&横向喜欢填字游戏。

在这里,我可以将horizontally作为:

进行比较
var arr1 = ["clue","hand","inst","land","door","or","lan","lue"];
var arr2 = [['c','l','u','e'],['h','a','n','d'],['i','n','s','t'],['l','a','n','d'],['d','o','o','r']];

for(var i=0; i < arr2.length;i  ){
    //compares whether any words from arr2 present in arr2 
    if(arr1[i].indexOf(arr2[i].join(''))> -1){
        console.log("isVetical",arr1[i]); 
//this prints all the matching element from arr1
    }
}

以同样的方式检查character by character vertically如何填字游戏。

提前致谢!

最佳答案:

1 个答案:

答案 0 :(得分:1)

使用Array.reduce()创建垂直文本,然后查看是否在其中找到arr1元素:

for(i = 0 ; i < arr1.length ; i  ){
  for(j = 0 ; j < 10 ; j  ) {
    vert= arr2.reduce(function(prev, curr) {return prev curr[j];}, '')
    if(vert.indexOf(arr1[i]) > -1) {
      output.innerHTML = "isVertical: " arr1[i] '\n'; 
    }
  }
}

<强>段

var arr1 = ["clue","hand","inst","land","door","or","lan","lue","child","sno"],
    arr2 = [['c','l','u','e'],['h','a','n','d'],['i','n','s','t'],['l','a','n','d'],['d','o','o','r']],
    output= document.querySelector('#output'),
    vert, i, j;

for(i = 0 ; i < arr2.length ; i  ){
  if(arr1[i].indexOf(arr2[i].join(''))> -1){
    output.innerHTML = "isHorizontal: " arr1[i] '\n'; 
  }
}

for(i = 0 ; i < arr1.length ; i  ){
  for(j = 0 ; j < 10 ; j  ) {
    vert= arr2.reduce(function(prev, curr) {return prev curr[j];}, '')
    if(vert.indexOf(arr1[i]) > -1) {
      output.innerHTML = "isVertical: " arr1[i] '\n'; 
    }
  }
}
<pre id="output"></pre>

本文经用户投稿或网站收集转载,如有侵权请联系本站。

发表评论

0条回复