1

I've the following code. The return value of the function get_last_catergory_value is always undeifned. I've searched stackoverflow but couldn't debug the issue.

When I show the value being returned just before the return statement it shows the correct value. But when it is returned from function it is undefined. Can you please help me to resolve this?

function fetch_product(brand) {
	var brand = brand;

	//get last category id
	var last_category = 'subcategory10';

	var last_category_value = get_last_catergory_value(last_category);
	alert(last_category_value); //undefined
}

function get_last_catergory_value(last_category) {

	if($('.' + last_category).find(':selected').val() == 'none') {

		last_category_number = last_category.substring(11);
		last_category_number = parseInt(last_category_number);
		last_category_number--;

		last_category = last_category.substring(0, 11);
		last_category = last_category + last_category_number;
		get_last_catergory_value(last_category);  //recall the function with values 'subcategory9', 'subcategory8' and so on...
	} else {
		var value = $('.' + last_category).find(':selected').val();

		alert(value); //Gives the correct value here
		return value;
	}
}

Forgive me if its a trivial issue. Thanks in Advance.

2
  • 2
    you're missing a return on the recursive call. Commented Oct 25, 2015 at 15:33
  • thanks. that solved the issue. Commented Oct 25, 2015 at 15:48

1 Answer 1

2

return statement is missing in the if block of get_last_catergory_value(last_category)

 function get_last_catergory_value(last_category) {

if($('.' + last_category).find(':selected').val() == 'none') {

    last_category_number = last_category.substring(11);
    last_category_number = parseInt(last_category_number);
    last_category_number--;

    last_category = last_category.substring(0, 11);
    last_category = last_category + last_category_number;
    return get_last_catergory_value(last_category);
} else {
    var value = $('.' + last_category).find(':selected').val();

    alert(value); //Gives the correct value here
    return value;
}
}
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.