繁体   English   中英

有没有办法检查字符串返回的次数?

[英]Is there a way to check how many times a string has been returned?

我目前正在摆弄JSfiddle的基本摇滚,纸张,剪刀游戏,我目前的目标是使应用程序通常更加用户友好。 有没有办法跟踪字符串返回的次数并显示该数字?

我从一个JSFiddle成员和Codecademy的练习中调整了这个,所以有些代码可能看起来很熟悉。

$('#begin').click(function() {
  playRPS();
  return false;
});

var playRPS = function() {
  var userChoice = prompt("Do you choose rock, paper or scissors?");
  var compChoice = computerChoice();
  var result = compare(userChoice, compChoice);
  $('body').append('<p>You chose ' + userChoice + '</p>')
    .append('<p>The computer chose ' + compChoice + '</p>')
    .append('<p>' + result + '</p>');
};

var computerChoice = function() {
  var randomNum = Math.random();
  if (randomNum < 0.34) {
    randomNum = "rock";
  } else if (randomNum <= 0.67) {
    randomNum = "paper";
  } else {
    randomNum = "scissors";
  }
  return randomNum;
}

var compare = function(choice1, choice2) {
  if (choice1 === choice2)
    return "The result is a tie!";
  if (choice1 === "rock") {
    if (choice2 === "scissors")
      return "You win";
    else
      return "The Computer wins";
  }
  if (choice1 === "paper") {
    if (choice2 === "rock")
      return "You win";
    else
      return "The Computer wins";
  }
  if (choice1 === "scissors") {
    if (choice2 === "rock")
      return "You win";
    else
      return "The Computer wins";
  }
  if (choice1 === "bomb") {
    return "Well Played, You win";
  }
};

您可以使用计数器来实现这一目标。 这些天我不是经常做这件事,但人们偶尔会使用它们。

let counter = 0;

if (true) {
  counter += 1;
}

console.log(counter); // Would log 1 to the console.

你可以循环遍历这个,每次传递条件时,它都会在名为counter的变量中将值加1。

在您的情况下,您可以在返回上方的行上为每个结果添加一个计数器。

嗯,是。 有多种方式。 如果你有一个返回多个字符串的函数,你可以使用一个对象来跟踪数字:

var counter = {}

var str = functionCall();

if (counter.hasOwnProperty(str))
 {
    counter[str] ++;
 }
else
{
   // Initialize the property before you can increment
   counter[str] = 1;
}

console.log(counter[str]);

这为您提供了一个拥有尽可能多的计数器的对象。 对于每个字符串,它比计数器变量更有利

只需将其计入全局变量:

 $('#begin').click(function() { playRPS(); return false; }); // we will keep count of what each player played in this object var counts = { computer: { rock: 0, paper: 0, scissors: 0, bomb: 0 }, player: { rock: 0, paper: 0, scissors: 0, bomb: 0 } }; var playRPS = function() { var userChoice = prompt("Do you choose rock, paper or scissors?"); // if the user choice is known, then it should have a number type entry in counts if (typeof counts.player[userChoice] !== 'number') { // user error handling $('body').append('<p>Unknown choice: ' + userChoice + '</p>'); return; } // increment count for current player choice counts.player[userChoice] += 1; var compChoice = computerChoice(); var result = compare(userChoice, compChoice); var pCount = counts.player[userChoice]; var cCount = counts.computer[compChoice]; $('body').append( '<p>You chose ' + userChoice + ' (' + pCount + ' time' + (pCount > 1 ? 's' : '') + ')' + '</p>') .append( '<p>The computer chose ' + compChoice + ' (' + cCount + ' time' + (cCount > 1 ? 's' : '') + ')' + '</p>') .append('<p>' + result + '</p>'); }; var computerChoice = function() { var randomNum = Math.random(); let choice = null; // gives a very little chance to computer of bombing you if (randomNum < 0.0001) { choice = "bomb"; } else if (randomNum < 0.34) { choice = "rock"; } else if (randomNum <= 0.67) { choice = "paper"; } else { choice = "scissors"; } // increment count for current computer choice counts.computer[choice] += 1; return choice; } var compare = function(choice1, choice2) { if (choice1 === choice2) return "The result is a tie!"; if (choice1 === "rock") { if (choice2 === "scissors") return "You win"; else return "The Computer wins"; } if (choice1 === "paper") { if (choice2 === "rock") return "You win"; else return "The Computer wins"; } if (choice1 === "scissors") { if (choice2 === "paper") return "You win"; else return "The Computer wins"; } if (choice2 === "bomb") { return "Computer bombed you, You lose"; } if (choice1 === "bomb") { return "Well Played, You win"; } }; 
 <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> <input type="button" id="begin" value="Begin"/> 

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM