For Loop in JavaScript - Lottery Website

后端 未结 2 1769
余生分开走
余生分开走 2021-01-24 03:35

I am trying to turn this code into a correct for loop statement, so that I can save my repetitions. I have tried my best to get it done, but I just don\'t know how I can write i

2条回答
  •  花落未央
    2021-01-24 04:24

    try something like this:

    Array.prototype.getDuplicates = function() {
      var cache = {}, results = [], that = this;
      that.forEach(function(item, index) {
          if(!cache.hasOwnProperty(item) && that.lastIndexOf(item) > index) {
              results.push(item);
          }
          cache[item] = true;
      });
      return results;
    }
    
    var answers = [luckyNumber, luckyNumber2, luckyNumber3];
    var indexes = [answers.indexOf(firstInput), answers.indexOf(secondInput), answers.indexOf(thirdInput)];
    if(indexes.indexOf(-1) === -1 && indexes.getDuplicates().length === 0) {
      // alert("Whatever");
    }
    

提交回复
热议问题