Stranger in Wonderland

Codecademy で 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
English

Chronological Archive

Tags