Skip to content

Commit

Permalink
Implement nthFibonacci with iterative solution
Browse files Browse the repository at this point in the history
  • Loading branch information
fay-jai committed Jan 1, 2015
1 parent 5aec2db commit f3fdcf5
Showing 1 changed file with 15 additions and 0 deletions.
15 changes: 15 additions & 0 deletions nthFibonacci/nthFibonacci.js
Original file line number Diff line number Diff line change
Expand Up @@ -43,5 +43,20 @@ var nthFibonacci = (function () {

// Iterative
var nthFibonacci = function (n) {
var previousOne, previousTwo, result;

if (n === 1 || n === 0) {
return n;
}

previousOne = 1;
previousTwo = 0;

for (var i = 2; i <= n; i += 1) {
result = previousOne + previousTwo;
previousTwo = previousOne;
previousOne = result;
}

return result;
};

0 comments on commit f3fdcf5

Please sign in to comment.