2016-07-12 45 views
0

我正在製作一些化學遊戲,將其作爲JavaScript中的一個Telegram機器人,其中包含庫存,您需要混合正確的化學物質。我的庫存包含在一個數組中,爲了防止通過簡單地粘貼庫存來強制你的方式達到某個級別,我需要檢查用戶輸入是否僅包含所需的化學物質,而不是包含所需的化學物質陣列。如何檢查一個字符串是否包含某個數組中的某個元素,但不包含其他元素

例如:

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."); 
} 

在海格的水平,你會得到一個更大的庫存,這將導致非常大的if語句這樣。這看起來很糟糕,必須有更好的方法。有沒有像獨家indexOf()或任何其他解決方案?我已經檢查了arr.filter()例如,但我找不到一個很好的方法來實現這一點。

+1

您是否嘗試過使用一個循環來遍歷數組了嗎? – nnnnnn

回答

1

手動檢查每種成分並不是一個好主意,因爲您指出如果配方的要求很長且庫存也很長,則會變得非常單調乏味。

我建議創建一個函數rightIngredients,它需要兩個數組:需求和使用的項目。

考慮到應該只使用食譜中的項目,我在函數內部要做的第一件事是檢查兩個數組的長度。如果它們不同,那麼它應該返回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 
+0

這是一個非常優雅的解決方案,它適用於我。這就是我一直在尋找的東西。我希望能夠發展自己想出的技能。 – bdbdbd

1

可以創建的所有化學品禁止的陣列,然後使用Array.every以檢查是否所有元素滿足該條件。你也會根據等級有不同的組合,所以我會建議創建一個關卡和禁用化學品的地圖,並使其具有通用性。

下面是一個示例:

// 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."); 
} 
+0

在一個關卡中有多個步驟,所以Marc Copte的回答是我最喜歡的,因爲每個步驟都要寫入所需的化學品,而不是禁用化學物質。 +1的努力。 – bdbdbd

相關問題