Anda di halaman 1dari 2

How Recursion Works: Sort of... here's what happens. Lets say you have dollar in change.

You run this program the first time. the computer asks is howMuchMoney less than 0.25? No, s o it goes to the else block and returns 1 + howManyQuarters(1.00 - 0.25). So, no w you have 0.75 in change. The program runs again, enters the else and this time returns 1 (from the original return call) + 1 + howManyQuarters(.75 - 0.25). Th is continues until the method enters the if block. In this example this is what would happen... It enters if block returns 0 (from the actual if return) + 1 (fr om the first return) + 1 (from the second return) + 1 (from the third return) + 1 (from the last return): 0 + 1 + 1 + 1 + 1 = 4. Or another way to show it is... return 1 + [howManyQuarters(1.00 - 0.25 = 0.75)] | \/_______/ | [1 + [howManyQuarters(0.75 - 0.25 = 0.50)]] | | \/_______/ | | [1 + [howManyQuarters(0.50 - 0.25) = 0.25]] | | | \/_______/ | | | [1 + [howManyQuarters(0.25 - 0.25) = 0.00]] | | | | \/______________________________/ | | | | [0] \/ \/ \/ \/ \/ return 1 + 1 + 1 + 1 + 0 = return 4 Where the square brackets represent another call to the same method and what tha t call equals. Hope this helps.

When you call this method you have to set three parameters, the coinsName, or an array of coin names, the currency, or an array that lists the amount that a giv en coin is equal to (For example: quarter = 0.25), and the index or the location in an array (in this the coinsName arrray and the currency array). The first th ing that happens after you have set all those values is it ask the question, is index greater than or equal to the length of the array currency, because you don 't want a value that isn't outside of the arrays (this cases an error). If the i ndex is greater than or equal to 5 (currency.length) than its done, the method r eturns nothing (return;). if it's not greater than or equal to 5, then the compu ter enters the else block and runs the method howManyCoins(coinNames[index], cur rency[index], 0). Once this method is done running the makeChange method calls i tself, increasing the index by one so that it can call the next type of money an d that money's value. This can be a little confusing so I'll show you what the c omputer is doing the first time... The makeChange method is called setting coinNames equal to the array coinNames.. . var coinNames = ["five dollar bills", "one dollar bills", "quarters", "dimes", " pennies"]; the currency parameter is then set to the array currency... var currency = [5.00, 1.00, 0.25, 0.10, 0.01]; finally, the index is set to 0 (or the start of any array). Now it asks the question is index (which is 0) greater than or equal to currency .length... if(index >= currency.length) if(0 >= 5) well 0 isn't greater or equal to 5 so the computer goes to the else block and ru

ns the howManyCoins method... howManyCoins(coinNames[index], currency[index], 0); howManyCoins(coinNames[0], currency[0], 0); howManyCoins("five dollar bills, 5.00, 0); this method runs 3 times before printing out... coinsSoFar + " " coinName 3 + " " + "five dollar bills" "3 five dollar bills" After this is printed out the makeChange method calls itself... makeChange(coinNames, currency, index + 1); makeChange(coinNames, currency, 0 + 1); makeChange(coinNames, currency, 1); Again it checks the to see if index is greater than or equal to the currency.len gth (still 5). Its not so, the computer grabs the 2nd value (since an arrays fir st value is anArray[0]) of coinNames, or "dollar bills", and the 2nd value of cu rrency, or 1.00. The method will keep calling itself, and increasing the index b y 1, until the index is greater than or equal to 5, thereby calling howManyCoins once for each variable inside the coinNames array, or each type of coin, or dol lar bill, and the given value, or the currency array, for that coin, or dollar b ill. Hope this helps.

Anda mungkin juga menyukai