Project: Recursive Functions
2012-11-13 TuesdayCodecademy で Project: Recursive Functions が終わった。 コンピュータに5枚の手札を配って、その中に特定のカードがあるかどうか当てるゲーム。ここまでは順調!
var cards = new Array("ace","king","queen","jack",10,9,8,7,6,5,4,3,2);
var hand = [];
function dealHand(numberOfCards){
//if numberOfCards is greater than zero
if(numberOfCards > 0){
// Store a random number
var n = Math.floor(Math.random() * cards.length);
// Add card to the hand array
hand.push(cards[n]);
// Output the card
console.log("Your card is " + cards[n]);
// remove card selected from cards array
cards.splice([n],1);
// remove from numberOfCards
numberOfCards--;
// recursive function call
dealHand(numberOfCards);
}
}
function goFish(num, guess){
// if num is greater than zero
if(num >= 0){
if(hand[num]===guess){
console.log("A Match for", hand[num]);
return;
} else if (num === 0){
console.log("Go Fish: No matches for " + guess);
}
// remove from num
num--;
// recursive function call... remember to use both arguments
goFish(num, guess);
}
}
// Call dealHand and goFish
dealHand(5);
goFish(cards.length, 3);
昨日の日報ポストのコメント欄より。
recursion と tail recursion と stack overflow の関係について聞いて、
Stack Overflow って洒落てんなー!
Google の検索結果“再帰”, “recursion”も洒落てんなー!
って思ったよ。
Author
Yuko Honda Morita (yukop) : yukop.com
飯能→東京→シリコンバレー。夫と猫2匹と暮らしてます。作ったり学んだり踊ったりするのが好き。
Born in Japan, living in California with my husband and two cats. "A bit of a geek and a bit of a geek fan and a bit of an artist." ->
Latest Posts
Japanese
- 2016-02-20 » フロントバンパーの外し方メモ - SUBARU XV Crosstrek 2014
- 2016-01-19 » Singular they
- 2015-12-21 » San Joaquin River NWR
- 2015-12-20 » San Luis NWR
- 2015-11-22 » EAD(労働許可証)更新 その1
- 2015-11-08 » Burrowing Owl アナホリフクロウ
- 2015-10-31 » Tire Pressure Warning
English
- 2014-10-18 » Halloween Decorations
- 2014-08-01 » Cat Parasite
- 2014-07-29 » How to Make Dumplings From Scratch
- 2014-07-25 » Moving to an Unfamiliar Land That Speaks a Foreign Language
- 2014-07-23 » Going to See a Dentist
- 2014-07-22 » Agent Cooper's Favorite Cherry Pie
- 2014-07-19 » It's Nice Living with Two Kittens