Try
function findCategory(categoryName) {
var trail = [];
var found = false;
function recurse(categoryAry) {
for (var i = 0; i < categoryAry.length; i++) {
trail.push(categoryAry[i].category);
// Found the category!
if ((categoryAry[i].category === categoryName)) {
found = true;
break;
// Did not match...
} else {
// Are there children / sub-categories? YES
if (categoryAry[i].children.length > 0) {
recurse(categoryAry[i].children);
if(found){
break;
}
}
}
trail.pop();
}
}
recurse(catalog);
return trail
}
Demo: Fiddle
Answer from Arun P Johny on Stack OverflowTry
function findCategory(categoryName) {
var trail = [];
var found = false;
function recurse(categoryAry) {
for (var i = 0; i < categoryAry.length; i++) {
trail.push(categoryAry[i].category);
// Found the category!
if ((categoryAry[i].category === categoryName)) {
found = true;
break;
// Did not match...
} else {
// Are there children / sub-categories? YES
if (categoryAry[i].children.length > 0) {
recurse(categoryAry[i].children);
if(found){
break;
}
}
}
trail.pop();
}
}
recurse(catalog);
return trail
}
Demo: Fiddle
the return stmt does work but remember it will be called everytime the loop unwinds and that's not what you are looking at. Example
// global scope
String matchingVariable;
int getMatch(index count, String input, String[] inputs){
if(isValid(input) || count < inputs.length){
// your condition is met and break
// assign your value to global scope variable
matchingVariable = input;
}else if(matchingVariable ==null){
++count
if(count < inputs.length){
getMatch(count, input+inputs[count], inputs)
}
// NOTE RETURN - I WOULDN'T DO THIS
return input;
// doesn't work instead assign the input to global scope variable when a match is found.
}
}
Break out of Javascript recursive function on .click()
how to break a recursive callback function in javascript - Stack Overflow
javascript - How to get out of Recursive function? - Stack Overflow
javascript - Recursive function break - Stack Overflow
You can't return within ternary operator. Even if it can, your recursion won't work because it will always return before arguments.callee is executed. Also, it's not recommended to use arguments.callee since it will throw an error in strict mode.
function main(callback) {
callback(3);
}
main(function redo(val) {
console.log(val);
if (val > 50) return;
redo(val + 1);
});
Your function should be like this
main(function(val) {
console.log(val);
if( val < 50 )
arguments.callee(val + 1);
});
And coming to the problem and the error
val > 50 ? return false: return true;
This line of code is wrong, the ternary operator must be used to assign a value to variable and not use it like a executable block.
var flag = val > 50 ? false: true;
Is valid.
Neither. Loop control statements like break and continue are only valid inside a loop (for, while, do, or switch). The break statement in your code is not in a loop, so this code will cause a syntax error.
The only way to "break out" of multiple levels of a recursive function is to throw an exception. This is probably not ideal for this situation; a better option for your situation may be to use a nested loop instead of recursion.
You could simply use return instead, and always check the result from the caller's perspective. In this case you'll need to return a different value when it "breaks":
function checkSwitch(arr2d, rates, n = 0, found = []) {
if (n === arr2d.length) {
inCase(found, rates);
return true;
}
for (let i in arr2d[n]) {
if (deepCheck(_switch, [...found, arr2d[n][i]])) {
if (checkSwitch(arr2d, rates, n + 1, [...found, arr2d[n][i]])) {
return true;
}
}
}
return false;
}
Here, returning true basically means "stop everything" (break out), and returning false means stop the current iteration normally (as OP explained himself in the comments below).
This does not technically "break out", but will instead roll back up the call chain before returning, which is (imho) the cleanest approach (though technically not as optimal as breaking in one go) if you're going to stick to a recursive function.