ODD-EVEN Number using Recursion in JS ?

Hello Guys, Today is this article we are taking about Recursion.
We are solving and understanding a problem by using Recursion in JavaScript.

? Problem :- Find odd-even number using Recursion ?

Let’s start today’s article…

• What is recursion?…


This content originally appeared on DEV Community and was authored by Shubham Jadhav

Hello Guys, Today is this article we are taking about Recursion.
We are solving and understanding a problem by using Recursion in JavaScript.

? Problem :- Find odd-even number using Recursion ?

Let's start today's article...

• What is recursion?
=> Recursion mean calling itself till condition not true. A function call itself that's function called Recursive function.

• How to find odd even number in JavaScript?
=> We are finding odd-even number by dividing 2. When the reminder is 0 it's mean the number is even or reminder is 1, it is odd number. Right...

We are using same logic in today's solving problem using recursion but different.

? Let's start solving problems

• First we create a function and pass one parameter. As following ?

img1

• We are using same logic as we use always to find odd even number but in this problem we are consider if the parameter's value is 0 it is even or if it's value is 1 it means number is odd.

• See following code to better understand, we are using if-else condition. ?
img2

• Now time to use recursion function but before using recursion we simply decrease parameter 'num' value by 2.
Because odd and even number difference is 2 for example 1 and 3 difference is 2 and also 2 and 4 difference is 2. See below ?
img3

• In the above code, in a line number 14 we are calling a function that's mean it will call itself till when given condition not true.

• Let's check the output ?
img4

? Thanks for reading...
? Stay with us for more....


This content originally appeared on DEV Community and was authored by Shubham Jadhav


Print Share Comment Cite Upload Translate Updates
APA

Shubham Jadhav | Sciencx (2021-09-25T05:54:10+00:00) ODD-EVEN Number using Recursion in JS ?. Retrieved from https://www.scien.cx/2021/09/25/odd-even-number-using-recursion-in-js-%f0%9f%9a%80/

MLA
" » ODD-EVEN Number using Recursion in JS ?." Shubham Jadhav | Sciencx - Saturday September 25, 2021, https://www.scien.cx/2021/09/25/odd-even-number-using-recursion-in-js-%f0%9f%9a%80/
HARVARD
Shubham Jadhav | Sciencx Saturday September 25, 2021 » ODD-EVEN Number using Recursion in JS ?., viewed ,<https://www.scien.cx/2021/09/25/odd-even-number-using-recursion-in-js-%f0%9f%9a%80/>
VANCOUVER
Shubham Jadhav | Sciencx - » ODD-EVEN Number using Recursion in JS ?. [Internet]. [Accessed ]. Available from: https://www.scien.cx/2021/09/25/odd-even-number-using-recursion-in-js-%f0%9f%9a%80/
CHICAGO
" » ODD-EVEN Number using Recursion in JS ?." Shubham Jadhav | Sciencx - Accessed . https://www.scien.cx/2021/09/25/odd-even-number-using-recursion-in-js-%f0%9f%9a%80/
IEEE
" » ODD-EVEN Number using Recursion in JS ?." Shubham Jadhav | Sciencx [Online]. Available: https://www.scien.cx/2021/09/25/odd-even-number-using-recursion-in-js-%f0%9f%9a%80/. [Accessed: ]
rf:citation
» ODD-EVEN Number using Recursion in JS ? | Shubham Jadhav | Sciencx | https://www.scien.cx/2021/09/25/odd-even-number-using-recursion-in-js-%f0%9f%9a%80/ |

Please log in to upload a file.




There are no updates yet.
Click the Upload button above to add an update.

You must be logged in to translate posts. Please log in or register.