Recursive Functions

function countDwon(n){
console.log(n)
if(n > 1){
countDwon(n -1) // recursive call
}
else {
return true // base case
}
}

How computers execute a recursive functions:

function sumUpTo(n){
if(n > 1){
sumUpTo(n - 1) + n
} else {
return 1
}
}

sumUpTo(5)
// this translates to
sumUpTo(4) + 5
// then
sumUpTo(3) + 4
sumUpTo(2) + 3
sumUpTo(1) + 2
// 1

--

--

--

Love podcasts or audiobooks? Learn on the go with our new app.

Recommended from Medium

SOLID Javascript

find and filter arrays in JavaScript and Node.js

Guide to create a simple React breadcrumb component using Webpack and publish to npm

Hoisting in JavaScript

A special way to dispatch Redux Actions — React-Redux

Use a button to upload files on your React App (with bootstrap)

Bringing two worlds together: my very first website API takeover hack

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Zainab Omar

Zainab Omar

More from Medium

Extending through Abstracting

How Prisoner’s Dilemma Influences Individual’s Actions

Arduino Lessons: Control Anything with 2-Axis Joystick

Bulk update using LIMIT in Oracle PL/SQL stored procedure