2016-10-12 5 views
2

先週、私はやっている簡単なプログラムにいくつかの問題があり、ここで誰かが私を助けました。今私は別の問題に遭遇しました。 は、私は現在、このコードを持っている:重み付けされた確率と値を使って配列内のアイテムを見つける

var findItem = function(desiredItem) { 
 
    var items = [ 
 
     { item: "rusty nail", probability: 0.25 }, 
 
     { item: "stone", probability: 0.23 }, 
 
     { item: "banana", probability: 0.20 }, 
 
     { item: "leaf", probability: 0.17 }, 
 
     { item: "mushroom", probability: 0.10 }, 
 
     { item: "diamond", probability: 0.05 } 
 
    ]; 
 
    var possible = items.some(({item, probability}) => 
 
      item === desiredItem && probability > 0); 
 
    if (!possible) { 
 
     console.log('There is no chance you\'ll ever find a ' + desiredItem); 
 
     return; 
 
    } 
 
    var sum = items.reduce((sum, {item, probability}) => sum+probability, 0); 
 
    while (true) { 
 
     var value = Math.random() * sum; 
 
     var lootedItem = items.find( 
 
       ({item, probability}) => (value -= probability) <= 0).item; 
 
     if (lootedItem === 'diamond') break; 
 
     console.log("Dang! A " + lootedItem + " was found..."); 
 
    } 
 
    console.log("Lucky! A " + desiredItem + " was found!"); 
 
} 
 

 
findItem('diamond');

今、私はitems配列にcategoryと呼ばれる別の値を追加することで、この上で拡大したいと思います。カテゴリの値を2,5または10のいずれかにします。したがってdiamondアイテムがcategory: 10に属し、findItemが実行された場合、同じカテゴリに属する​​アイテムだけが見つかるとします。私は数日のうちに努力してきましたが、その周りに頭を浮かべているようには見えません。誰かが私を正しい方向に押し込むのを助けるかもしれないのでしょうか?事前に感謝

あなたはそのコードにこの更新プログラムを使用することができ
+0

['filter'](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/filter)を使用して検索を特定のカテゴリに限定しますか? –

+0

「ダン!」メッセージは、そのカテゴリに対してのみ発生する可能性があり、他のすべての項目は完全に無視されるべきであることがわかりますか? – trincot

+0

ありがとう、私はそれを調べます! –

答えて

3

// Pass the item list and the desired category as arguments: 
 
var findItem = function(items, category, desiredItem) { 
 
    // apply filter to items, so only those of the given category remain: 
 
    items = items.filter(item => item.category == category); 
 
    // rest of code remains the same: 
 
    var possible = items.some(({item, probability}) => 
 
      item === desiredItem && probability > 0); 
 
    if (!possible) { 
 
     console.log('There is no chance you\'ll ever find a ' + desiredItem); 
 
     return; 
 
    } 
 
    var sum = items.reduce((sum, {item, probability}) => sum+probability, 0); 
 
    var t = 10; 
 
    while (true) { 
 
     var value = Math.random() * sum; 
 
     var lootedItem = items.find( 
 
       ({item, probability}) => (value -= probability) <= 0).item; 
 
     if (lootedItem === desiredItem) break; // fixed this condition! 
 
     console.log("Dang! A " + lootedItem + " was found..."); 
 
     t--; if (t <= 0) throw "loop"; 
 
    } 
 
    console.log("Lucky! A " + desiredItem + " was found!"); 
 
} 
 

 
// Define items here with their category 
 
var items = [ 
 
    { item: "rusty nail", probability: 0.25, category: 2 }, 
 
    { item: "stone",  probability: 0.23, category: 2 }, 
 
    { item: "banana",  probability: 0.20, category: 2 }, 
 
    { item: "leaf",  probability: 0.17, category: 5 }, 
 
    { item: "mushroom", probability: 0.10, category: 5 }, 
 
    { item: "diamond", probability: 0.05, category: 10 } 
 
]; 
 

 
// Call function with extra arguments: 
 
findItem(items, 5, 'mushroom'); 
 

 
console.log('second run:'); 
 
// This will obviously give a hit immediately, as there is only one possible item: 
 
findItem(items, 10, 'diamond');

変更は以下のとおりです。あなたの関数へ

  • パス以上の引数:アイテムリストと目的のカテゴリ
  • fuの最初のアクションとしてアイテムリストにフィルタを適用する関数
  • lootedItemテストに関する問題を修正しました。「ダイヤモンド」はハードコードされていました。
  • 関数外の項目リストを定義し、各要素にカテゴリ値を追加します。
  • 余分な引数を渡す関数の呼び出しを修正します。
+0

おかげさまで多くのおかげで、すべてのビットが何をしたのかを説明してくれてありがとう。今コーディングについてもっと知っているような気がする! –

1

このようなものが欲しいですか?

var items = [ { item: "rusty nail", probability: 0.25, category: 10 } 
 
      , { item: "stone",  probability: 0.23, category: 5 } 
 
      , { item: "banana",  probability: 0.20, category: 2 } 
 
      , { item: "leaf",  probability: 0.17, category: 5 } 
 
      , { item: "mushroom", probability: 0.10, category: 2 } 
 
      , { item: "diamond", probability: 0.05, category: 10 } 
 
      ]; 
 

 
findItem("diamond", items); 
 

 
function findItem(needle, haystack) { 
 
    var item = haystack.find(thing => thing.item === needle && 
 
             thing.probability > 0); 
 

 
    if (item) { 
 
     var category = item.category; 
 
     var items = haystack.filter(thing => thing.category === category); 
 
     var sum = items.reduce((sum, thing) => sum + thing.probability, 0); 
 

 
     var value = sum * Math.random(); 
 
     var loot = items.find(thing => (value -= thing.probability) <= 0).item; 
 

 
     while (loot !== needle) { 
 
      value = sum * Math.random(); 
 
      console.log("Dang! A " + loot + " was found..."); 
 
      loot = items.find(thing => (value -= thing.probability) <= 0).item; 
 
     } 
 

 
     return console.log("Lucky! A " + needle + " was found!"); 
 
    } 
 

 
    console.log("There's no chance that you'll ever find a " + needle); 
 
}

あなたのコードからの唯一の大きな違いは、私が検索を制限するためにfilterを使用したことです。

+0

それを指摘してくれてありがとう! :) –

関連する問題