Recursion function javaScript [duplicate] - javascript

This question already has answers here:
Recursive function returns undefined
(3 answers)
Closed 1 year ago.
i tried to write this function for guessing if a given number is even or not using recursion,
This is my code :
function isEven(N){
if(N===0){
return true;
}else if(N===1){
return false;
}else {
isEven(N-2);
}
}
console.log(isEven(1)); // gives false
console.log(isEven(0)); //gives true
console.log(isEven(10)); // gives undefined
I dont know why the function is not working except for 0 and 1.

Because the isEven() funtion enters a loop and cannot send anything to the output. Try to change your function and write it in other way

Related

a javascript exercise that imitates "include()" [duplicate]

This question already has answers here:
Function with forEach returns undefined even with return statement
(5 answers)
Why does this forEach return undefined when using a return statement
(5 answers)
Short circuit Array.forEach like calling break
(30 answers)
Closed 8 days ago.
I'm learning the basics of javascript and I ran into this exercise which confused me a bit. The exercise is to manually create a method like "includes." So when I implement it like that:
function includes(num,arr) {
arr.forEach(val => {
if (val===num) return true;
return false;
})
}
const arrr=[1,2,3,4];
console.log(arrr.includes(2));
it works.
but when I implement it like that, which makes much more sense to me:
function includes(num,arr) {
arr.forEach(val => {
if (val===num) return true;
return false;
})
}
const arrr=[1,2,3,4];
console.log(includes(2,arrr));
, it doesn't and the output is "undefined."
Can someone explain me the logic behind it? the first way doesn't even matches the functions arguments and works, while the second way does match the arguments and doesn't.
Thanks.

Why does If statement throw an undefined error when I am checking if its undefined? [duplicate]

This question already has answers here:
Access Javascript nested objects safely
(14 answers)
Closed 4 years ago.
Why does my else if statement cause the parser to throw an undefined error? I know that it is undefined which is why I am checking, I want it to hit the else block in this case, because this function is used in mutiple places the data that is being passed in is not always the same. So I am trying to check which type of data is being passed in.
if(icon){
dv.find(".mobCol>.image").html(generateIcon(icon));
}
else if(location[0].properties !== undefined){
dv.find(".mobCol>.image").html(generateIcon(location[0].properties.image));
data = location[0];
}
else{
dv.find(".mobCol>.image").html(generateIcon(location.features[0].properties.image));
data = location.features[0];
}
Because location[0] is undefined. It's a bit odd, but your check should be:
if( location[0] && location[0].properties){ /* .. */}
I've omitted the ===undefined, thats not needed. It tests if it is something is thruthy or falsey
This is a common way to check variables in JS.

Why does console.log("hello") inside console return undefined? [duplicate]

This question already has answers here:
Why JavaScript functions always return a value?
(4 answers)
Closed 5 years ago.
Out of curiosity, why does writing console.log("hello") inside console return undefined?
Is it for the same reasons in defining void function in C?
The console.log just write a text and return with undefined.
If you create a function, add a returning value, there will be not undefined, it will returns with the added value.
Example:
function writer(){
console.log("write new line");
return "ok";
}
If you call the writer() that the output is "ok" in new line after "write new line".

Toggle boolean using function? [duplicate]

This question already has answers here:
Does JavaScript pass by reference? [duplicate]
(13 answers)
Closed 6 years ago.
I would like to create a single function to toggle any boolean of my code, but I'm not able to get the expected result :
function toggle(x){
x=!x;
}
var test=false;
toggle(test);
alert(test);
Why doesn't this return true ?
Boolean datatype is passed by value. So, any changes made to the argument x will not reflect the actual variable test. You need to return the updated value from the function and assign it back to the variable.
function toggle(x) {
return !x; // Return negated value
}
var test = false;
test = toggle(test); // Assign the updated value back to the variable
alert(test);
But, as said in comments, it's better to use
test = !test;

node.js: Is console.log() a function? [duplicate]

This question already has answers here:
Why is the method executed immediately when I use setTimeout?
(8 answers)
Closed 8 years ago.
Exactly, is console.log() a function? Why do these following two code snippets have different output?
function delay(x) {
console.log('Start of timeout');
return x;
};
setTimeout(function(){console.log('End of timeout');}, delay(5000));
console.log('Start to do something else');
.
function delay(x) {
console.log('Start of timeout');
return x;
};
setTimeout(console.log('End of timeout'), delay(5000)); // ???????
console.log('Start to do something else');
Yes, console.log is a function.
The first snippet uses the expected syntac for setTimeout. The second calls console.log inline, which returns undefined. That comes the first argument to setTimeout. That explains the different timing of when End of timeout appears.

Categories