简体   繁体   English

如何检查字符串是否包含数组中的某个元素,但不包含其他元素

[英]How to check if a string contains a certain element from an array, but not the other elements

I am making some chemistry game as a Telegram bot in JavaScript where you have an inventory and you need to mix the right chemicals. 我正在用JavaScript作为Telegram机器人来做一些化学游戏,在这里您有库存,并且需要混合正确的化学物质。 I have the inventory contained in an array, and in order to prevent brute-forcing your way to a level by simply pasting your inventory, I need to check if the user input contains the needed chemicals exclusively, and not any of the others in the array. 我将库存包含在一个数组中,为了防止仅通过粘贴库存就将您强行带入一个水平,我需要检查用户输入的内容是否仅包含所需的化学品,而不是其中的任何其他化学品。数组。

For example: 例如:

users[id].inventory = ["Beaker", "Water", "Baking soda", "Heating plate", "Hydrochloric acid"];

if (users[id].level === 1 && 
    msg.text.toLowerCase().indexOf('baking soda') !== -1 &&
    msg.text.toLowerCase().indexOf('hydrochloric acid') !== -1 && 
    msg.text.toLowerCase().indexOf('beaker') === -1 && 
    msg.text.toLowerCase().indexOf('water') === -1 && 
    msg.text.toLowerCase().indexOf('heating plate') === -1) {

    msg.answer("You mix some baking soda with hydrochloric acid.\nSome fun fizzing happens and you produce useless CO2 gas.");
}

At higer levels you will get a much larger inventory and this will lead to very large if-statements this way. 在较高级别,您将获得更大的库存,并且这将导致以这种方式生成非常大的if语句。 This looks bad and there must be a better way. 这看起来很糟糕,必须有更好的方法。 Is there such thing like an exclusive indexOf() or any other solution? 是否有诸如独占indexOf()类的东西或任何其他解决方案? I have checked out arr.filter() for example but I can't find out a good way to implement this. 例如,我已经签出了arr.filter() ,但是我找不到实现此目的的好方法。

Checking for every ingredient manually is not a good idea, as you point out it will become very tedious if the requirements for a recipe are long and the inventory is also long. 手动检查每种成分并不是一个好主意,因为您指出,如果对配方的要求很长并且库存也很长,它将变得非常乏味。

I suggest creating a function rightIngredients that takes two arrays: the requirements and the items used. 我建议创建一个函数rightIngredients ,它接受两个数组:需求和使用的项目。

Considering that only the items in the recipe should be used, the first thing I'd do inside the function is to check the length of both arrays. 考虑到只应使用配方中的项目,因此在该函数中要做的第一件事是检查两个数组的长度。 If they are different then it should return false and nothing else needs to be checked. 如果它们不同,则应返回false,而无需检查其他任何内容。

If the length of the arrays is the same, then we check each of the items used to see if they are in the requirements. 如果数组的长度相同,那么我们将检查用于检查它们是否符合要求的每个项目。 If one of them is not, then we return false as well. 如果其中之一不是,那么我们也返回false。

requirements = ["baking soda", "hydrochloric acid"];

function rightIngredients(req, uses) {
    if (uses.length != req.length) {
    console.log(uses.join(', ')+' are not even the right amount of ingredients');
    missing = true;
  } else {
    var i = 0;
    var missing = false;
    while (i<uses.length && !missing) {
        if (req.indexOf(uses[i].toLowerCase())===-1) missing = true;
      ++i;
    }
    if (missing) console.log(uses.join(', ')+' are not the right ingredients');
    else console.log(uses.join(', ')+' are the right ingredients');
  }
  return !missing;
}
rightIngredients(requirements, ["Beaker", "Baking Soda", "Hydrochloric Acid"]);
// Beaker, Baking Soda, Hydrochloric Acid are not even the right amount of ingredients
rightIngredients(requirements, ["Beaker", "Baking Soda"]);
// Beaker, Baking Soda are not the right ingredients
rightIngredients(requirements, ["Baking Soda", "Hydrochloric Acid"]);
// Baking Soda, Hydrochloric Acid are the right ingredients

You can create an array of all disallowed chemicals and then use Array.every to check if all elements satisfy this condition. 您可以创建所有不允许使用的化学药品的数组,然后使用Array.every检查所有元素是否满足此条件。 Also you will have different combination based on level, so I would suggest to create a map of level and disallowed chemicals and make the function generic. 另外,您将基于级别使用不同的组合,因此我建议创建一个级别和禁用化学物质的图,并使该功能通用。

Following is a sample: 以下是一个示例:

// Map object of Level and Chemicals
var map = [{
  level: 1,
  disallowed_chemicals: ['baking soda', 'hydrochloric acid', 'beaker', 'water', 'heating plate']
}];

// getter function to get map object for current level.
// If this returns undefined you can assume, incorrect level is entered
function getLevelMap(u_level) {
  return map.find(function(o) {
    return o.level === u_level;
  });
}

var m_level = getLevelMap(users[id].level);
if (m_level &&
  m_level.every(function(ch) {
    return msg.toLowerCase().indexOf(ch) < 0;
  })) {

  msg.answer("You mix some baking soda with hydrochloric acid.\nSome fun fizzing happens and you produce useless CO2 gas.");
}

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

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