JavaScript:比较两个数组并打印匹配数

时间:2016-11-14 05:49:08

标签: javascript arrays string-comparison

我在这里,我必须比较两个数组,并显示总共有多少匹配,但是不起作用,我不知道为什么。我真的很感激这个提示

/*creating the function to check the lottery results */
function checkNumbers(_customerNumber, _winningNumers) {
    var matches = 0
    for (var i = 0; i < getCustomerNumber.length; i++) {
      for (var j = 0; j < getCustomerNumber.length; j++) {
        if (getCustomerNumber[i] == getWinningNumbers[j]) matches++;
      }
    }
    return matches;
  }
  /*getting the customer number*/

function getCustomerNumber() {
  return ['12', '17', '24', '37', '38', '43'];
}

/*array to return the winning numbers*/

function getWinningNumbers() {
  return ['12', '17', '24', '37', '38', '43'];
}

function displayResult() {
  var winningMessage = "This Week's Winning Numbers are:\n" + getWinningNumbers() + "\n";
  var customerMessage = "The Customer's Number is:\n" + getCustomerNumber() + "\n";
  var numbersMatched = "Numbers Matched\n" + checkNumbers
  alert(winningMessage + customerMessage + numbersMatched);
}

function init() {
  displayResult();
}
window.onload = init;

4 个答案:

答案 0 :(得分:1)

您可以使用Array.filter获取已过滤的列表,并使用.indexOf检查另一个数组中是否存在值。

&#13;
&#13;
let arr1 = ['12', '17', '24', '37', '38', '43'],
  arr2 = ['22', '47', '24', '38', '38', '44'];

var matches = arr1.filter(function(item){
  return arr2.indexOf(item) > -1
})
console.log(matches)
console.log("Total matches: ", matches.length)
&#13;
&#13;
&#13;

window.onload也不是一个好习惯。这将取代所有以前的处理程序。你应该使用

window.addEventListener('load', function(){...})

您的代码已更新

&#13;
&#13;
/* creating thefunction to check the lottery results */
function checkNumbers(_customerNumber, _winningNumers) {
    return _customerNumber.filter(function(x) {
      return _winningNumers.indexOf(x)
    }).length
  }
  /* getting the customer number */

function getCustomerNumber() {
  return ['12', '17', '24', '37', '38', '43'];
}

/* array toreturn the winning numbers */

function getWinningNumbers() {
  return ['12', '17', '24', '37', '38', '43'];
}

function displayResult() {
  var winNum = getWinningNumbers();
  var custNum = getCustomerNumber();
  var winningMessage = "This Week's Winning Numbers are:\n" + winNum + "\n";
  var customerMessage = "The Customer's Number is:\n" + custNum + "\n";
  var numbersMatched = "Numbers Matched\n" + checkNumbers(custNum, winNum)
  console.log(winningMessage, customerMessage, numbersMatched);
}

function init() {
  displayResult();
}
window.addEventListener('load', init);
&#13;
&#13;
&#13;

答案 1 :(得分:1)

在没有()的情况下调用函数的一个常见错误。

另一个是你应该在javascript中声明你的所有函数,因为这是一种解释语言,在使用它之前应首先声明函数。

你的代码应该是这样的。

&#13;
&#13;
function getWinningNumbers() {
  return ['12', '17', '24', '37', '38', '43'];
}

function getCustomerNumber() {
  return ['12', '17', '24', '37', '38', '43'];
}

function checkNumbers() {
    var matches = 0;
    var customerNumbers = getCustomerNumber();
    var winningNumbers = getWinningNumbers();

    for (var i = 0; i < customerNumbers.length; i++) {
      for (var j = 0; j < winningNumbers.length; j++) {
        if (customerNumbers[i] == winningNumbers[j]) {
          matches++;
        }
      }
    }
    return matches;
  }
  /*getting the customer number*/

function displayResult() {
  var winningMessage = "This Week's Winning Numbers are:\n" + getWinningNumbers() + "\n";
  var customerMessage = "The Customer's Number is:\n" + getCustomerNumber() + "\n";
  var numbersMatched = "Numbers Matched\n" + checkNumbers();
  console.log(winningMessage + customerMessage + numbersMatched);
}

function init() {
  displayResult();
}
window.onload = init;
&#13;
&#13;
&#13;

答案 2 :(得分:0)

let arr1 = ['12', '17', '24', '37', '38', '43'],
    arr2 = ['22', '47', '24', '38', '38', '44'];

function getMatch(arr1, arr2) {
    let matched = 0;
    arr1.forEach(i => {
        if (~arr2.indexOf(i)) {
            matched++;
        }
    });
    return matched;
}

答案 3 :(得分:0)

尝试使用像这样的function()。所有函数都是如此。应用所有地方的函数声明。现在正在工作。

var matches = 0
function checkNumbers (_customerNumber,_winningNumers)
{
 

for (var i=0;i<getCustomerNumber().length;i++) {
    for (var j=0;j<getWinningNumbers().length;j++) {
        if(getCustomerNumber()[i]==getWinningNumbers()[j]) {matches++; console.log(getWinningNumbers()[i])}
      
    }
}
return matches; 
}
/*getting the customer number*/

function getCustomerNumber()
{
return ['12','17','24','37','38','43'];
}

/*array to return the winning numbers*/

function getWinningNumbers ()
{
return ['12','17','24','37','38','43'];
}
function displayResult()
{
var winningMessage =  "This Week's Winning Numbers are:\n" +      getWinningNumbers()+ "\n";
var customerMessage = "The Customer's Number is:\n" + getCustomerNumber()+ "\n";
var numbersMatched = "Numbers Matched\n" + checkNumbers();
alert(winningMessage + customerMessage + numbersMatched);
}
function init()
{
displayResult();
}
window.onload = init;

替代 试试这是函数checkNumbers()这两个函数都没有得到长度。所以尝试使用数组工作

var matches = 0;
function checkNumbers ()
{

for (var i=0; i<getCustomerNumber.length;i++) {
    for (var j=0;j<getCustomerNumber.length;j++) {
        if(getCustomerNumber[i]==getWinningNumbers[j]){ matches ++;}
     
    }
}
return matches;
}
/*getting the customer number*/

var getCustomerNumber = ['12','17','24','37','38','43'];


var getWinningNumbers = ['12','17','24','37','38','43'];

function displayResult()
{
var winningMessage =  "This Week's Winning Numbers are:\n" +      getWinningNumbers+ "\n";
var customerMessage = "The Customer's Number is:\n" + getCustomerNumber+ "\n";
var numbersMatched = "Numbers Matched\n" + checkNumbers();
alert(winningMessage + customerMessage + numbersMatched);
}
function init()
{
displayResult();
}
window.onload = init;