AngularJS Example:
by Sezer Ekinci
HTML
<link rel="stylesheet" href="http://twitter.github.com/bootstrap/assets/css/bootstrap.css">
<div ng-app>
<div ng-controller="profitCtrl">
<div style="margin-bottom:10px;">
<input type="text" ng-model="start" placeholder="Start"/>
<input type="text" ng-model="profit" placeholder="Profit"/>
<input type="text" ng-model="week" placeholder="Week"/>
<button ng-click="calculate()">Calculate</button>
</div>
<div ng-repeat="result in results" style="border-top:1px solid #dedede;">
<span style="display:inline-block;width:80px;">Week {{$index + 1}}: </span>
<span style="display:inline-block;width:100px;text-align:right">$ {{result | number : 2}}</span>
</div>
</div>
</div>
CSS
input,button {width:110px;}
JavaScript
function profitCtrl($scope,$timeout) {
$scope.start = 100;
$scope.profit = 21.380683;
$scope.week = 52;
$scope.results = [];
var result, actualWeek = 1;
$scope.calculate = function(){
actualWeek = 1;
$scope.results = [];
result = keep(angular.copy($scope.start));
$scope.results.push(result);
}
function keep(amount) {
if (actualWeek < $scope.week-1) {
$timeout(function() {
result = keep(result);
$scope.results.push(result);
actualWeek++;
}, 300)
}
return parseFloat((amount * parseFloat((((100 * $scope.profit) / $scope.start) / 100) + 1)).toFixed(2));
}
}