Writing a recursive JavaScript function returns "undefined" - javascript

I am trying to learn recursive functions in JS but for some reason the result is undefined I want to alert (4,3,2,1,0) respectively.
function someFun(number) {
if (number < 0) {
return 1;
}
else {
alert(number = someFun(number - 1))
}
}
someFun(4)

Your function only has one return statement in it. When the else path of the if statement is taken, nothing is returned.
That said, you really don't need a return value because you just want the sequence of numbers printed (in descending order):
function someFun(number) {
if (number >= 0) {
alert(number);
someFun(number - 1);
}
}
someFun(4);
If you want the numbers printed in ascending order, you'd just reverse the order of the alert() and the recursive call.

Related

return statement that returns nothing in recursion

I found this code about recursion online
function countDownRecursive(n) {
if (n <= 0) {
console.log('Hooray')
return
}
console.log(n)
countDownRecursive(n - 1)
}
I am really confused about this code, why does it console.log("Hooray") and then return nothing? Can you explain it to me? Thank you so much.
you returned a null value, the function output type is void.
try this
if (n <= 0) {
console.log('Hooray')
return n
}
return in this context means you don't want to continue running the function (similar to break in iterations).
The above recursive function's logic can be converted to this below while logic.
let n = 3;
//iterate until found the while break
while (true) {
//the condition to stop
if (n <= 0) {
console.log('Hooray');
break; //stop `while`
}
console.log(n)
n = n - 1;
}
why does it console.log("Hooray")
Because the function is recursive and when you start with let's say n=1 the function will not print "Hooray" immediately, because the condition:
if (n <= 0)
does not apply i.e. is false.
By the time we reach the recursion:
countDownRecursive(n - 1)
We call the function again with n=0 due to n - 1, the if-statement will evaluate to true and therefore print "Hooray".
and then return nothing
It does not actually return "nothing", even though the return type is void, it returns undefined, which is the default behavior for return you could also write return undefined instead.
When you use return, it will basically terminate or return from the current function. It will jump back into the scope where you did call the function initially.
Hope that clears it up for you.
Let's do it with an example. This is the hierarchy of the calls when n = 5
countDownRecursive(5) // "5"
countDownRecursive(4) // "4"
countDownRecursive(3) // "3"
countDownRecursive(2) // "2"
countDownRecursive(1) // "1"
countDownRecursive(0) // "Hooray" because n == 0, we execute the return statement
end of countDownRecursive(0) because of return
end of countDownRecursive(1) because reaching the end
end of countDownRecursive(2) because reaching the end
end of countDownRecursive(3) because reaching the end
end of countDownRecursive(4) because reaching the end
end of countDownRecursive(5) because reaching the end
The returns statement tells the program to stop calling itself

find the length of a string without using loops, built in methods, or .length property in javascript?

I have been stuck on this problem for quite sometime. In addition to not using built in methods and the length property, we cannot use any loops either, which is a giveaway that this problem must be solved with recursion. I have tried this function but I am still stuck.
function getLength(string, length = 0){
if (string[0] === undefined) {return length};
length++;
return getLength(length);
}
console.log(getLength("hello"))
// expected answer: 5
You’re very close.
function getLength(string, length = 0){
if (string[length] === undefined) {return length};
length++;
return getLength(string, length);
}
console.log(getLength("hello"))
// expected answer: 5
You already figured the answer you just missed few parameters
function getLength(string, length = 0) {
if (string[length] === undefined) {
return length
};
length++;
return getLength(string, length);
}
console.log(getLength("hello"))
You can define the base case of the recursion which would terminate the recursion, as when either the string which is passed is empty/undefined/null or the the index at which you are in the recursive process has exceed the length of the given string in which case you would return 0.
Then call the function recursively by incrementing the index of the string and adding 1 in each recursive process till you hit the base condition:
function getLength(str, idx = 0) {
//base case
if (!str || !str[idx]) {
return 0;
}
return 1 + getLength(str, idx + 1);
}
console.log(getLength("hello"));

Factorial recursion, giving undefined

I am trying to simply find recursion factorial
function factorial(num, result) {
console.log('num', num)
if (num === 0) {
console.log('res ', result);
return result;
} else {
result = result * num;
console.log(result)
factorial(--num, result);
}
}
let res = factorial(3, 1)
console.log(res)
It is giving undefined, not sure why, need some help.
plnkr
Bside the missing return statement, some annotations:
Use a default value for the result. This enables to call the function without given a starting value and allows touse only a single parameter.
Take only one early exit of the recursive call, without using else in combination with return. The return statement omits the following else statement. Just go on with the code without an else part.
Return at the end of the function a recursive call to allow to use the compiler to take a TCO (tail call optimization). This wotks without extending the stack, because the last call is replace with the actual recursive call. This optimization may not actually implemented.
Do not use decrement operators, if you need only the reduced value without using the reduced value again.
Move the calculation into the function call.
function factorial(num, result = 1) {
console.log('num', num);
if (num === 0) return result;
return factorial(num - 1, result * num);
}
console.log(factorial(3))

Eloquent JavaScript 2nd Edition recursion exercise solution

I attempted to solve the recursion exercise of the online book eloquentjavascript 2nd edition:
Here is what the question states:
We’ve seen that % (the remainder operator) can be used to test whether
a number is even or odd by using % 2 to check if it’s divisible by
two. Here’s another way to define whether a (positive, whole) number
is even or odd:
Zero is even.
One is odd.
For any other number N, its evenness is the same as N - 2.
Define a recursive function isEven corresponding to this description.
The function should accept a number parameter and return a boolean.
Test it out on 50 and 75. See how it behaves on -1. Why? Can you think
of a way to fix this?
Here is what I have attempted and it works:
function isEven(number) {
if (number == 0) {
return true;
} else {
return (-number % 2 == 0) ? true : false;
}
return isEven(number - 2);
}
console.log(isEven(-1));
But, it seems to be a wrong answer because as I understood the author wants me to use - 2 method. I am wondering if this is indeed the correct answer or if it is wrong could someone point me in the right direction.
I think the reason you are getting confused is because you're not understanding how recursion works. The reason it is n-2 is, it is taking the number and subtracting 2 until it is either a zero or one. Therefore giving us a true or false.
Hope that helps. Read over how the process of recursion works.
The alternative solution that doesn't use the modulos operator % or any other built in functions in JavaScript is provided below. This solution instead relies on using another recursion to change negative value of the number.
function isEven(number) {
if (number < 0) {
return isEven(-number);
} else if (number == 1) {
return false;
} else if (number == 0) {
return true;
} else {
return isEven(number - 2);
}
}
console.log(isEven(50)); // true
console.log(isEven(75)); // false
console.log(isEven(-1)); // false
We call isEven(number -2) to go back to the top of the function with the number that was inputed intially but 2 less than before and it will keep doing that until the number is 1 or 0 and then it will be able to return the boolean true or false (even or odd).
I think the problem is to create an isEven function without using a mod/%/remainder operation
function isEven(number) {
if (number < 0) {
number = Math.abs(number);
}
if (number===0) {
return true;
}
if (number===1) {
return false;
}
else {
number = number - 2;
return isEven(number);
}
}
I guess it is important to add to this
it doesn't handle strings
it doesn't handle floats
don't put this into a production application
function isEven(n) {
n = Math.abs(n);
if (n==0)
return true;
else if (n==1)
return false;
else
return isEven(n-2);
}
console.log(isEven(-50)); // → true
console.log(isEven(75)); // → false
console.log(isEven(-75)); // → false
console.log(isEven(-1)); // → false
var isEven = function(n) {
// Get the absolute value of the number
// so we don't have to bother with negatives
n = Math.abs(n);
// We know that if we subtract 2 from our number
// until it is equal to zero, our number was even
// If our number isn't zero, this statement will be skipped
if(n === 0) return true;
// We know that if we subtract 2 from our number
// and it reaches a value of 1, it isn't even
// If our number isn't 1, this statement will be skipped
if(n === 1) return false;
// We subtract 2 from our original number and pass it
// back in to this function to check it over and over again
// until one of the conditions is met and the function can
// return a result
return isEven(n - 2);
}
// We test our function here
console.log(isEven(-21));
This is the leanest method I could come up with. Notice we call our isEven function from within itself and pass in the value of n - 2. This will constantly subtract 2 from our number and check to see if it is equal to 0 or 1 until we get a proper result and return the corresponding boolean.
I hope this clears things up a little.
How about the below code? Seems to work for me.
/*if > 1 keep looking, otherwise return reverse boolean value.
If negative value, inverse, to answer the last part of the question...
Also needed to use parseInt as the text value I used,
was considered a string value!
This being a recursive function which really isn't necessary,
means that in javascript we will get a
stack size exceeded error when the number becomes too large.*/
function isEven(x) {
return (x>1)?isEven(x-2):(x>=0)?!x:isEven(-x);
}
The book wants the following console.log outputs for such values
console.log(isEven(50));
// → true
console.log(isEven(75));
// → false
console.log(isEven(-2));
// → ??
The hint says "When given a negative number, the function will recurse again and again, passing itself an ever more negative number, thus getting further and further away from returning a result. It will eventually run out of stack space and abort." So I'm not sure if what I got represents that but my code and console.log outputs is shown below:
let isEven = function(num) {
if (num ===0) {
return true
} else if (num===1){
return false
} else {
return isEven(num-2)
}
}
console.log(isEven(50));
// → true
console.log(isEven(75));
// → false
console.log(isEven(-2));
// → RangeError: Maximum call stack size exceeded (line 3 in function isEven)```
The question requires that you don't' not to use the modulus operator (%).
// Your code here.
var isEven = function(a){
if (a == 0){ //case 1 : zero is even
return true;
}
else if(a == 1){ //case 2: one is odd
return false;
}
else {
return isEven(Math.abs(a-2)); //solves case 3 (negative numbers)
}
}
console.log(isEven(50));
// → true
console.log(isEven(75));
// → false
console.log(isEven(-1));
// → ?? ....false
This is the "correct" answer from the website. I say "correct" in quotes because I don't understand the 2nd 'else if' statement's return isEven(-n). Why did the author include the option to turn n into a positive number?
function isEven(n) {
if (n == 0)
return true;
else if (n == 1)
return false;
else if (n < 0)
return isEven(-n);
else
return isEven(n - 2);
}
Anyways, thought I'd share since I didn't see anybody post THIS answer.
//define recursive function isEven takes a positive whole number
//returns true if even
function isEven(x) {
//define innner function to loop over value
function find(current) {
//check if value == 0, return true
if (current == 0) {
return true
//check if value == 1 return false
} else if (current == 1) {
return false
//loop over value, subtracting 2 each time until either 0 or 1
//which returns the approriate true/false value based on evenness
} else {
console.log(current)
return find(current -2)
}
}
return find(x)
}
If the number is 1 or -1 so ODD, the function returns false,
If the number is 0 so EVEN, it returns true,
If then number nor 1,-1 nor 0 we have to first check if the number is positive or negative,
If the number is negative and less than -1 we call the function again but increase the number by 2 until we get -1 or 0;
If the number is positive and greater than 1 we call the function again but decrease the number by 2 until we get 1 or 0;
So for example, the number is 5 ->it`s positive and greater than 1 so call the function over and over again and decrease the number by 2 until the result will be 0 or 1:
5->3->1 //->false
function isEven(num) {
if (num == 1 || num == -1) {
return false;
} else if (num == 0) {
return true;
} else {
if (num < -1) {
return isEven(num + 2);
} else {
return isEven(num - 2);
}
}
}
Test result:
console.log(`-35 is even: ${isEven(-35)}`); //-> false
console.log(`-1 is even: ${isEven(-1)}`); //-> false
console.log(`0 is even: ${isEven(0)}`); //-> true
console.log(`32 is even: ${isEven(32)}`); //-> true
I see quite a few examples above using math functions and modulo operator. But I guess the author expects a simple recursive function to make the reader understand the functioning of the same and how if not properly defined it can lead to repetitive function calls and eventually lead to overflow of stack. I believe this code below can help you understand:
function isEven(n){
if(n==0){
return true;
} else if(n==1){
return false;
} else {
return isEven(n-2);
}
}
console.log(isEven(50));
console.log(isEven(75));
console.log(isEven(-1));
function isEven(num){
if(num %2 == 0) {
return true;
} else {
return false;
}
}
console.log(isEven(50));
// → true
console.log(isEven(75));
// → false
console.log(isEven(-1));
// → ??
function isEven(number) {
while(number>= -1)
{
return isEven(number-2) == 0 && number>-1 ? true : false;
}
}
console.log(isEven(-1));
I hope this helps:
const evenOrOdd = (N) => {
if (Math.abs(N) === 0) {
return console.log('Even');
} else if (Math.abs(N) === 1) {
return console.log('Odd');
}
evenOrOdd(Math.abs(N) - 2);
};
Google Chrome Console code results:
It utilizes all three given arguments on the problem. It can handle negative integers. It does not use the modulo operator. Lastly, I wasn't able to see any code snippet similar to what I provided thus I am enticed to share what I can.

recursive function return different values depending of order when summing array elements

I implemented the following recursive JS function to get the the sum of elements in a array. This function works fine, when input [1,2,3] it returns 6, which is OK.
function sumOfNumbers(array) {
if (array.length == 1) {
return array[0];
} else {
last = array.length - 1;
return array[last] + sumOfNumbers(array.slice(0, last));
}
}
However, when changing the order of the sum to:
return sumOfNumbers(array.slice(0,last)) + array[last];
It returns 5 for [1,2,3]. Does anybody knows why?
Because the variable last is global, and you're changing it with your call to sumOfNumbers(array.slice(0,last)), before this part: array[last] sees it.

Categories