recursion - Issue with recursive function in javascript -



recursion - Issue with recursive function in javascript -

i practicing using recursive functions solve simple javascript problems.

i running issue next code,

var locate = function(arr,value){ (var i=0; <arr.length; i++) { if(typeof arr[i]=== "string") { console.log("string string"); if(arr[i]=== value) { console.log("this should true"); homecoming true; } } else { locate(arr[i], value); } } } console.log(locate(['d', 'a',['e', 'g']], 'e'));

i cannot programme homecoming true. gets right part of code, prints statement above it.

any help appreciated. have been banging head @ couple of hours now.

edit-@matt burland pointed out fact need include homecoming statement when calling recursive.

when recurse, need return value returned recursive phone call unwind.

so in else clause, need:

return locate(arr[i], value);

class="snippet-code-js lang-js prettyprint-override">var locate = function(arr,value){ (var i=0; <arr.length; i++) { if(typeof arr[i]=== "string") { console.log("string string"); if(arr[i]=== value) { console.log("this should true"); homecoming true; } } else { homecoming locate(arr[i], value); } } } alert(locate(['d', 'a',['e', 'g']], 'e'));

javascript recursion

Comments

Popular posts from this blog

Delphi change the assembly code of a running process -

json - Hibernate and Jackson (java.lang.IllegalStateException: Cannot call sendError() after the response has been committed) -

C++ 11 "class" keyword -