3号宾果游戏。我怎么能把它写得更好更短?

时间:2015-12-13 18:55:39

标签: javascript

所以我从他们希望让用户输入1到100之间的3个数字的课程中完成了简单的任务,并重复它直到用户数高于70.

我做了这个:https://github.com/itayJoseph/Javascript-Bingo/blob/master/Bingo%20V2.js



//Bingo : User puts 3 numbers and they must be all equal to the computer 3 numbers
//Gets 3 Numbers from User
function getUserNum() {
  var userNum = [];
  for (var i = 0; userNum.length < 3; i++) {
    userNum[i] = prompt("insert one number at a time ranging from 1 to 100");
    if (userNum[i] > 100 || userNum[i] < 1 || userNum[i] == '') {
      alert("Inavlid Number");
      comparison();
    } else {
      userNum[i] = parseInt(userNum[i]);
    }
  }
  console.log(userNum);
  return userNum;
}

//Generate numbers from 1 to 100
function numG(x, y) {
  var bingo;
  bingo = Math.random() * (y - x) + x;
  return parseInt(bingo);
}

//Get 3 Numbers from computer
function compNum() {
  var bingoNum = [];

  for (var j = 0; bingoNum.length < 3; j++) {
    bingoNum[j] = numG(1, 100);
  }

  return bingoNum;
}

/*
This last function does the job of getting the 3
numbers from user then generating its own 3 numbers untill
all 3 numbers are equal.(follow the console to see the progress)
*/
function comparison() {
  var userNumbers = getUserNum();
  var bingoNumbers;

  if (userNumbers.length == 3) {
    do {
      var numMatches = 0;
      bingoNumbers = compNum();
      console.log(bingoNumbers);
      for (var i = 0; i < 3; i++) {
        for (var j = 0; j < 3; j++) {
          if (userNumbers[i] == bingoNumbers[j]) {
            numMatches += 1;
          }
        }
      }
    } while (numMatches != 3)
  }

  if (numMatches == 3) {
    alert("BINGOOOO");
  }
}
comparison();
&#13;
&#13;
&#13;

0 个答案:

没有答案