Recurring function returning undefined - javascript

I have a function that checks if a number is divisible by 2.5, if it isn't it adds 0.5 then calls itself again with the new value. It returns undefined when I call it like below but if I console.log instead of returninside the function then the correct rounded up value displays.
function n(num){
if(num % 2.5 === 0){
return num;
}else{
num += 0.5;
n(num)
}
}
console.log(n(13.5))
Any ideas?

you need to return the recursive call so that your conditions will be checked again until your if condition is satisfied:
function n(num){
if(num % 2.5 === 0){
return num;
}else{
num += 0.5;
return n(num); //<-- add return
}
}
console.log(n(13.5));

Yes because the second n(num) call is not returning anything to the original caller.
Make it:
else{
num += 0.5;
return n(num);
}

Related

Check if number is not integer in recursion js [duplicate]

This question already has answers here:
How to check if a variable is an integer in JavaScript?
(41 answers)
Closed 1 year ago.
I have a recursion function which calculates factorial for a given number.
function findFactorial(num) {
if (num < 1 || !num.isInteger) {
return "error";
} else {
return num * findFactorial(num - 1);
}
}
console.log(findFactorial(7));
I want to check if number is less then 1 OR is not integer - and return "error" for it.
Code I tried above isn't working and giving me "error" every time function is called.
You have three issues in your code.
First your code will always return "error", since you will always come to the point where num == 0.
Second you have no termination condition (except for the wrong return "error"), so the recursion will never return a valid value.
And last but not least, you try to call a function isInteger() but this function does not exist for the num object.
Here is the valid code:
function findFactorial(num) {
if (isNaN(num) || num < 1) {
return "error";
}
if (num == 1) {
return 1;
}
return num * findFactorial(num - 1);
}
console.log(findFactorial(7));
num.isInteger function is not availble in js. We have Number.isInteger(val).
Some code changes needed.
When a number is not an integer or is a negative number then only we need to return the error.
When a number reaches 1 then we need to return 1 itself to get then factorial values.
See the below code.
function findFactorial(num) {
if (!Number.isInteger(num) || num < 0) {
return "error";
} else if (num <= 1) {
return 1;
}
else {
return num * findFactorial(num - 1);
}
}
console.log(findFactorial(7));
console.log(findFactorial(-5));
console.log(findFactorial("six"));

javascript code, create a function that returns a boolean depending on whether or not a number is even or not?

var i = 1;
var numberCounter = 0
function isEven(num){
if (i % numberCounter === 0) {
console.log('true');
} else { console.log('false');
}
console.log(isEven(50));
console.log(isEven(75));
console.log(isEven(-1));
Im not exactly sure where i went wrong here, i am trying to create a function that returns a boolean depending on whether or not a number is even or not.
in order to check if a number is even or not you should check to see if the modulo of 2 with the number is 0. other then that in order to return a value from a function you should use the keyword return. therefor the isEven function should be:
function isEven(num){
return num % 2 === 0;
}
console.log(isEven(50));
console.log(isEven(75));
console.log(isEven(-1));
(even number) % 2 = 0 and with ! you will the 0 as 1 in boolean.
% => module
/ => division
const isEven = number => !(number % 2)
isEven(2) // true
const isOdd = number => !!(number % 2)
isEven(3) // true
In your code :
var i = 1;
var numberCounter = 0
function isEven(num){
if (i % numberCounter === 0) {
console.log('true');
} else { console.log('false');
}
Note that i is equal to 0 and numberCounter is equal to 0. No matter which number is passed in your function, its value won't be considered.
You want to define if a number is odd or even. As a matter of fact the number you want to estimate is the parameter of the function.
A basic definition would be that an odd number can be divided by 2. It means that the remainder of the division is equal to 0.
the if statement concerns the num (passed as parameter) and the remainder.
I took your code and modified it. Saar Davidson answer is much more better and effective (in a single line).
function isEven(num){
if (num % 2 === 0) {
console.log('true');
return true;
} else {
console.log('false');
return false;
}
}

Trying to understand why function is returning undefined in recursive function

I am having trouble understanding whats happening in this recursive function.
Why does y === undefined??
function f(num){
if(num !== 10){
f(num + 1);
} else {
return num;
}
}
var y = f(0);
console.log(y);
If I log "num" right before its returned the value is 10.
Here is a jsfiddle:
https://jsfiddle.net/
if num is not 10, you just call f again. The value returned from f is not allocated anywhere. You should return that value.
function f(num){
if(num !== 10){
return f(num + 1); //you were returning nothing except when num reached 10
} else {
return num;
}
}
var y = f(0);
console.log(y);
You should rather return f(num + 1) inside if block.
Without which it returns nothing except when "num" reaches 10.
function f(num){
if(num !== 10){
return f(num + 1);
} else {
return num;
}
}
var y = f(0);
console.log(y);
// 10

trying to understand if/else loops combined with functions

so I am trying to create a function with two parameters. This function will be passed with two numbers as arguments. calculate the sum of the parameters. If the sum is less than or equal to 25, the function should return true. If not it should return false.
I know I must use a if and else loop to check the parameters (or a ternary which we have not covered yet.) This is what I have gotten so far. Please tell me if I am on the right track or completely wrong.
function sum(augment1,augment2) {
var num= augment1 + augment2;
return num;
}
var a=sum(10,30)
console.log(a);
if (num > 25) {
return true
}
else {
return false;
}
There's no reason to use an if-else statement - the <= operator returns a boolean result, so you could just return it:
function isSumEqualOrLessThan25(augment1, augment2) {
var sum = augment1 + augment2;
return sum <= 25;
}
Your conditions should be inside your function right? So if the sum is less than or equal to 25 return true else return false. Can you try following?
function sum(augment1, augment2) {
var num = augment1 + augment2;
if (num <= 25) {
return true
} else {
return false;
}
}
var a = sum(10, 30)
console.log(a); // should be false

What is wrong with my javascript recursion?

What is wrong with this recursion function?
fac(5) gives NaN
function fac(num){
while(num>2){
return(num*fac(num-1))
}
}
fac(1) returns nothing, which is undefined, and undefined*1 is NaN, that's why.
Change your code to
function fac(num){
return num>2 ? num*fac(num-1) : 1;
}
The correct way should be:
function fact(num) {
if(num > 2)
return num * fact(num - 1);
else
return num;
}
NaN - not a number error , occurs usually but not limited to when we try to apply a numerical operation to a value which is not a number.
The Issue at hand is when the input value is less than 2 , the function return undefined, which is not a number, so the return (num*fac(num-1)) will fail due to num * undefined.
to fix this , we have to return a value when number is 2 or less than 2.
function fact(num) {
if(num > 2)
return num * fact(num - 1);
else
return num;
}
What's wrong with your code
function fac(num){
while(num>2){
return(num*fac(num-1))
}
}
No return value for num <= 2. So, at some point your code will multiply Number with undefined which results in NaN
while loop is not needed
function fac(num) {
return (num > 2) ? num * fac(num - 1) : 1;
}
alert(fac(5));

Categories

Resources