31
loading...
This website collects cookies to deliver better user experience
A function that calls itself until it doesn't.
const countDownLoop = (count) => {
for (let i = count; i >= 0; i--) {
console.log(i)
}
}
const countDownRecurse = (count) => {
if (count === 0) {
return 0
}
console.log(count)
return countDownRecurse (count - 1)
}
if (count === 0) return 0
. Whitout this condition the function would call itself until it crashes. If the count is not zero the function will continue, first by logging count to the console. The it returns itself with count - 1.const factorial = (num) => {
if (num === 1) {
return 1
}
return num * factorial(num - 1)
}
const list = [1, 2, 3, 4, 5]
const search = ([head, ...tail], key) => {
/* if the head is undefined it means that
the key is not in the array and we return false */
if (typeof head === "undefined") {
return false
}
/* If the head is equal to the key we turn true
otherwise we call search again with the tail. */
return head === key ? true : search(tail, key)
}
console.log(search(list, 11)) // False
head = 1
tail = [2, 3, 4, 5]
1 === 11 // false
so we call search again, this time we pass in the tail as the list argument.head = 2
tail = [3, 4 ,5]
const list = [2, 1, 5, 4, 3]
const quickSort = ([head, ...tail]) => {
// Stopping condition
if (typeof head === 'undefined') {
return []
}
/* We split the tail in to two seperate list.
One that is smaller then the pivot (head)
and one that is larger or equal to head */
const smaller = tail.filter((num) => num < head)
const largerOrEqual = tail.filter((num) => num >= head)
return [...quickSort(smaller), head, ...quickSort(largerOrEqual)]
}
console.log(quickSort(list)) // [1, 2, 3, 4, 5]
const tree = {
value: 5,
left: null,
right: {
value: 8,
left: {
value: 6,
left: null,
right: null
},
right: {
value: 11,
left: null,
right: null
}
}
}
const sumElements = (tree) => {
if (tree === null) {
return 0
}
return tree.value + sumElements(tree.left) + sumElements(tree.right)
}
console.log(sumElements(tree)) // 30
const list = [2, 1, 1]
const checkFn = (x) => x === 1
const every = ([head, ...tail], fn) => {
/* if the head is undefiend it means that all
calls to fn(head) returned true or the list is empty */
if (typeof head === "undefined") {
return true
}
/* As long as fn(head) returns true we call the function again.
If fn(head) returns false the
function returns false instead of itself */
return fn(head) ? every(tail, fn) : false
}
console.log(every(list, checkFn)) // false
const some = ([head, ...tail], fn) => {
/* if the head is undefiend it means that
all calls to fn(head) returned false
or it was called on a empty list. */
if (typeof head === "undefined") {
return false
}
/* if fn(head) returns false it means it didn't fulfilled
the test function and we call some() again with a new head.
If any entry (head) in the list fulfills the
test function provided we return true */
return !fn(head) ? some(tail, fn) : true
}
const list = [1, 2, 3, 4, 5]
const dubble = (x) => x * 2
const map = ([head, ...tail], fn) => {
if (typeof head === 'undefined') {
return []
}
return [fn(head), ...map(tail, fn)]
}
console.log(map(list, dubble)) // [2, 4, 6, 8, 10]
const list = [1, 2, 3, 4, 5]
const evenNumbers = (num) => num % 2 === 0
const filter = ([head, ...tail], predicate) => {
if (typeof head === 'undefined') {
return []
}
return predicate(head) ? [head, ...filter(tail, predicate)] : filter(tail, predicate)
}
console.log(filter(list, evenNumbers)) // [2, 4]
const list = [1, 2, 3, 4, 5]
const sum = (x, y) => x + y
const reduce = ([head, ...tail], fn, acc) => {
if (typeof head === 'undefined') {
return acc
}
return reduce(tail, fn, fn(acc, head))
}
console.log(reduce(list, sum, 0)) // 15
Recursion is when a function calls itself until some condition stops it.
It can be used instead of a loop.
If there's no condition to stop the function, it'll recurse forever and crash your program so make sure to add it.