Created
April 5, 2015 00:11
-
-
Save bran921007/2e6d6ba7d9cbff91ee92 to your computer and use it in GitHub Desktop.
House of card Kata - Recursive function - Sequence - Series (sumatorias)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
http://stackoverflow.com/questions/27383686/how-could-i-have-made-this-ruby-kata-solution-into-a-recursive-method | |
https://laracasts.com/discuss/channels/general-discussion/house-of-cards-kata | |
http://cifrasyteclas.com/2012/12/07/cuantas-cartas-hacen-falta-para-construir-un-castillo-de-naipes-reto-77-propiedad-1/ | |
Description: | |
You want to build a standard house of cards, but you don't know how many cards you will need. Write a program which will count the minimal number of cards according to the number of floors you want to have. For example, if you want a one floor house, you will need 7 of them (two pairs of two cards on the base floor, one horizontal card and one pair to get the first floor). Here you can see which kind of house of cards I mean: http://www.wikihow.com/Build-a-Tower-of-Cards | |
Details (Ruby & JavaScript) | |
The input must be an integer greater than 0, for other input raise an error. | |
//Respuesta | |
function houseOfCards(floors){ | |
var cards =0; | |
if(floors <0){ | |
return null; | |
}else if(floors==0){ | |
return 2; | |
}else{ | |
cards += 3*floors + 2; | |
return cards + houseOfCards(floors - 1); | |
} | |
} | |
alert(houseOfCards(3)); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment