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));
Related
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"));
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;
}
}
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
Using return 1:
function factorial(n){
if(n===0){
return 1; //or return true;
}
return n * factorial(n-1);
}
->factorial(5);
<-120
Using return 0:
function factorial(n){
if(n===0){
return 0; //or return false;
}
return n * factorial(n-1);
}
->factorial(5);
<-0
Using return:
function factorial(n){
if(n===0){
return; // I don't know what it returns (true or false)?
}
return n * factorial(n-1);
}
->factorial(5);
<-NaN
So, what are the differences between return, return 0 or false, return 1 or true?
You're essentially forming a chain of multiplication operations:
factorial(3) * factorial(2) * factorial(1) ..
Which boils down to
3 * 2 * 1 ..
That return value you're changing will be the last part of this chain. If this last part is 1, you'll get your regular result. If the last part is 0, the result of the whole thing is 0, because anything multiplied by 0 is 0. If you're not explicitly returning anything, the last value is undefined, and a multiplication operation with something that is not a number yields the value NaN (Not a Number), which essentially means the result has no meaning.
function factorial(n){
if(n===0){
return 1; // actually here return 1 is necessary because it is using for multiplication in "n"
}
var x = factorial(n-1) ; //when n value will be n=1 then it return 1 so last return will be 5X4X3X2X(1 not 0 or true or false)
return n*x;
}
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);
}