Sunday, 15 March 2015

Recursive Function in JavaScript - Not calling it correctly -



Recursive Function in JavaScript - Not calling it correctly -

can help explain me doing wrong on recursive function? attempting pass in value , if value exists in list of options, homecoming it. if not available, want subtract 10000 , seek 1 time again until find matching value. it's pretty much working, except when find matching value, doesn't seem update on original function call, maybe? not sure. help appreciated. in advance.

var totalcost = 34000, options = [10000, 0], recamt; if (totalcost >= 30000) { recamt = getrecamt(30000, 1); console.log(recamt); } function getrecamt(value, tier) { console.log('trying: ' + value + ' ' + tier); var i2 = 0, recamt = 0; (i2 = 0; i2 < options.length; i2 += 1) { if (value === parseint(options[i2])) { recamt = value; console.log('found: ' + recamt); } } if(recamt === 0) { if (tier === 1) { getrecamt(value - 10000, tier); } } else { homecoming recamt; } }

currently console returns

trying: 30000 1 trying: 20000 1 trying: 10000 1 found: 10000 0

you're not returning anything

if(recamt === 0) { if (tier === 1) { getrecamt(value - 10000, tier); } } else { homecoming recamt; }

needs be

if(recamt === 0) { if (tier === 1) { // homecoming statement here homecoming getrecamt(value - 10000, tier); } } else { homecoming recamt; }

here's snippet demonstrate

class="snippet-code-js lang-js prettyprint-override">var totalcost = 34000, options = [10000, 0], recamt; if (totalcost >= 30000) { recamt = getrecamt(30000, 1); console.log(recamt); } function getrecamt(value, tier) { console.log('trying: ' + value + ' ' + tier); var i2 = 0, recamt = 0; (i2 = 0; i2 < options.length; i2 += 1) { if (value === parseint(options[i2])) { recamt = value; console.log('found: ' + recamt); } } if(recamt === 0) { if (tier === 1) { homecoming getrecamt(value - 10000, tier); } } else { homecoming recamt; } }

javascript

No comments:

Post a Comment