0

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 

1 Answer 1

3

You're not returning anything

if(recAmt === 0) {
    if (tier === 1) {
        getRecAmt(value - 10000, tier);
    }

} else {
    return recAmt;
}

needs to be

if(recAmt === 0) {
    if (tier === 1) {
        // RETURN STATEMENT HERE
        return getRecAmt(value - 10000, tier); 
    }

} else {
    return recAmt;
}

Here's a snippet to demonstrate

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) {
            return getRecAmt(value - 10000, tier);
        }

    } else {
        return recAmt;
    }
}

Sign up to request clarification or add additional context in comments.

6 Comments

The OP can also get rid of one of the if statements to say if(recAmt === 0 && tier === 1)
@Adjit - That would give a different result, albeit at the moment there is the chance that this method could return undefined from thismethod (when recAmt === 0 but tier is not 1).
Sorry, I'm not understanding. I made an edit to my original post to show what my console is logging. If (recAmt === 0) I am recalling my function and don't care about the value yet. I only care about it when a match is found, then I want that value. Does that make sense?
@Nick what is your desired output?
@Nick - The nature of a recursive function means that you always want to return the result of the next recursion, should the next recursion call itself, you want that result returned up the call chain. Try it with the change I described in this answer and see if it gives your expected output.
|

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.