There are many reasons why eval should be avoided, that it breaks scope is one of them. Passing a string to setTimeout causes it to be evaled when the timer runs out.
You should pass a function instead.
window.setTimeout(rotateImages(start+1),3000);
This calls rotateImages immediately, then passes its return value to setTimeout. This doesn't help since rotateImages doesn't return a function.
You probably want:
window.setTimeout(rotateImages,3000,[start+1]);
Or create an anonymous function that wraps a closure around start and pass that instead:
window.setTimeout(function () { rotateImages(start + 1); },3000);
The latter option has better support among browsers.
Answer from Quentin on Stack OverflowThere are many reasons why eval should be avoided, that it breaks scope is one of them. Passing a string to setTimeout causes it to be evaled when the timer runs out.
You should pass a function instead.
window.setTimeout(rotateImages(start+1),3000);
This calls rotateImages immediately, then passes its return value to setTimeout. This doesn't help since rotateImages doesn't return a function.
You probably want:
window.setTimeout(rotateImages,3000,[start+1]);
Or create an anonymous function that wraps a closure around start and pass that instead:
window.setTimeout(function () { rotateImages(start + 1); },3000);
The latter option has better support among browsers.
Be wary of code from W3Schools.
The other answers give a solution. I'll just add that you're recreating the Arrays and repeating the DOM selection every time the rotateImages function is called. This is unnecessary.
You can change your code like this:
(function() {
var a = ["image1.jpg","image2.jpg","image3.jpg", "image4.jpg"];
var c = ["url1", "url2", "url3", "url4"];
var b = document.getElementById('rotating1');
var d = document.getElementById('imageurl');
function rotateImages(start) {
b.src = a[start];
d.href = c[start];
window.setTimeout(function() {
rotateImages( ++start % a.length );
}, 3000);
}
rotateImages(0);
})();
recursion - JavaScript Recursive Callback Function - ReferenceError: function is not defined - Stack Overflow
javascript - Recursive function returns undefined - Stack Overflow
Recursive function returns undefined instead of the number
javascript 'function not defined' error in recursion - Post.Byes
In this arm of your function:
if (taxWage > minWage) {
// calculates tax recursively calling two other functions difference() and taxStep()
tax = tax + difference(taxWage) * taxStep(taxWage);
var newSalary = taxWage - difference(taxWage);
taxes(tax, newSalary);
}
you are not returning a value from the function or setting returnTax. When you don't return anything, the return value is undefined.
Perhaps, you want this:
if (taxWage > minWage) {
// calculates tax recursively calling two other functions difference() and taxStep()
tax = tax + difference(taxWage) * taxStep(taxWage);
var newSalary = taxWage - difference(taxWage);
return taxes(tax, newSalary);
}
There is a bug with your recursion:
taxes(tax, newSalary);
You don't return anything when the condition in the if evaluates to true. You need to change that to:
return taxes(tax, newSalary);
You have the necessary return statement in the else.
I have a recursive function that seems to be returning undefined even though console.log indicates that the return statement in the function should have a value.
function fearNotLetter(str) {
function checkChar(char) {
var tempStr = str.split("");
if (tempStr[char + 1] == String.fromCharCode(tempStr[char].charCodeAt(0) + 1)) {
checkChar(char + 1);
} else {
console.log(String.fromCharCode(tempStr[char].charCodeAt(0) + 1));
return String.fromCharCode(tempStr[char].charCodeAt(0) + 1);
}
}
console.log(checkChar(0));
}My first console.log statement indicates that the statement does get the value I'm expecting, but when I return that value, my second console.log statement gives me "undefined".
Can someone explain why the following code:
function factorial(n, sum) {
if (n === 0) {
return sum;
}
sum = sum * n;
factorial(n-1, sum);
}Returns undefined, when called
console.log(factorial(5, 1));
If I log just before returning sum, it says 120 as it should be.
I noticed if I do return facto...() it returns 120. Why is that?
You have to return function itself.
I asked this question in IRC and one of the guys comments helped me some:
it DOES return it. but it returns it to the function where n is 1. and that function doesn't return ANYTHING to n === 2, which returns nothing to n === 3, which returns nothing to n ===4, which returns nothing to your initial call of factorial(5, 1)
Because the for loop never executes when the element doesn't have children, due to both num and elementChildrenLength being 0 and the condition num<elementChildrenLength never being true.
if elementChildrenLength is equal to zero then the entire for loop block will not execute.
best way to avoid this is to wrap the for loop block with something along the lines of:
if(elementChildrenLength==0){
return 0;
}else{
for(var num=0; num<elementChildrenLength; num++) {
//for loop block here
}
}
If the above code shown is inside controller then write this.
const a = component.get('c.removeEmptyChildNodes');
$A.enqueueAction(a);
Or else if the above code is from helper then do this, but make sure helper is also passed as parameter in getData(cmp, event, helper) from controller:
helper.removeEmptyChildNodes(JSON.parse(temojson));
this might not work because setCallback has a function, so using this might limit the scope to that function.
I think problem is when you call removeEmptyChildNodes the argument your are passing is getting undefined for some reason. You can try something like this to execute your recursive function.
return this.removeEmptyChildNodes(d._children);
A "naked" return statement is effectively the same as
return undefined;
In the other case, your function has no return at all, so that's also like
return undefined;
Thus in all cases your function returns undefined.
It doesn't return a value; therefore it returns undefined.
function foo() {
return;
}
console.log(foo());
function bar() {}
console.log(bar());