Can someone help explain to me what I am doing wrong on this recursive function? I am attempting to pass in a value and if that value exists in my list of options, return it. If it is not available, then I want to subtract 10000 and try again until I find my matching value. It's pretty much working, except when I find my matching value, it doesn't seem to update on the original function call, maybe? Not sure. Any help would be greatly appreciated. Thanks 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 + ' for ' + tier);
var i2 = 0,
recAmt = 0;
for (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 {
return recAmt;
}
}
Currently my console returns
Trying: 30000 for 1
Trying: 20000 for 1
Trying: 10000 for 1
Found: 10000
0