-1

Hi I have tried this function .This function will give be row number till 'b' exist in same column

var arr= ['abc','tbu','iop'];

var getPositionbyRow = function(row, col) {
if(arr[row][col]!=='b') {
        return row;
    }

    getPositionbyRow(row+1,col);
}
var  r = getPositionbyRow(0,1);

but value of r is always undefined. can someone help me to understand why its happening. Thanks for help.

1
  • Please do not update question based on suggestions in answer/comments. This makes them void Commented Feb 9, 2018 at 6:48

2 Answers 2

1

In order for your recursive function to work you should return getPositionbyRow(row+1,col);

Also it is a good practise to add a condition when 'b' is not present in your array, and return -1 for example. That way you will avoid endless loops

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

1 Comment

Thanks realized mistake.
0

You only return something if arr[row][col]!=='b'. Otherwise you return nothing.

You need to

return getPositionbyRow(row+1,col);

1 Comment

Thanks realized mistake.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.