Skip to content

Commit

Permalink
Implement memoization
Browse files Browse the repository at this point in the history
  • Loading branch information
igorcervac committed Jun 20, 2024
1 parent 20e4f6f commit d1d1feb
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 0 deletions.
11 changes: 11 additions & 0 deletions memoization/index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Memoization</title>
</head>
<body>
<script src="index.js"></script>
</body>
</html>
23 changes: 23 additions & 0 deletions memoization/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
function fibbonacci(n){
if (n<=1){
return n;
}
return fibbonacci(n-1) + fibbonacci(n-2);
};

function memo(func){
let cache = {};

return function(n){
if (n in cache){
result = cache[n];
}
else {
result = func(n);
cache[n] = result;
}

return result;
}
}
console.log(memo(fibbonacci)(5));

0 comments on commit d1d1feb

Please sign in to comment.