var GRADE = [ { rating: 1, flight: "C", ppr: 0, mpr: 0 }, { rating: 2, flight: "C", ppr: 40, mpr: 1.3 }, { rating: 3, flight: "C", ppr: 45, mpr: 1.5 }, { rating: 4, flight: "CC", ppr: 50, mpr: 1.7 }, { rating: 5, flight: "CC", ppr: 55, mpr: 1.9 }, { rating: 6, flight: "B", ppr: 60, mpr: 2.1 }, { rating: 7, flight: "B", ppr: 65, mpr: 2.3 }, { rating: 8, flight: "BB", ppr: 70, mpr: 2.5 }, { rating: 9, flight: "BB", ppr: 75, mpr: 2.7 }, { rating: 10, flight: "A", ppr: 80, mpr: 2.9 }, { rating: 11, flight: "A", ppr: 85, mpr: 3.1 }, { rating: 12, flight: "A", ppr: 90, mpr: 3.3 }, { rating: 13, flight: "AA", ppr: 95, mpr: 3.5 }, { rating: 14, flight: "AA", ppr: 102, mpr: 3.75 }, { rating: 15, flight: "AA", ppr: 109, mpr: 4 }, { rating: 16, flight: "SA", ppr: 116, mpr: 4.25 }, { rating: 17, flight: "SA", ppr: 123, mpr: 4.5 }, { rating: 18, flight: "SA", ppr: 130, mpr: 4.75 } ]; var Result = function() { this.ppr1 = null; this.mpr = null; this.ppr2 = null; }; var Average = function() { var first = null; var second = null; var total = null; }; var myApp = angular.module( "myApp", [ 'ngCookies' ] ); myApp.filter('setDecimal', function ($filter) { return function (input, places) { if (isNaN(input)) return 0; var factor = "1" + Array(+(places > 0 && places + 1)).join("0"); return Math.round(input * factor) / factor; }; }); myApp.controller( "init", [ "$scope", "$cookies", "$cookieStore", function( $scope, $cookies, $cookieStore ) { $scope.grade = GRADE; //get Cookie var averageList = $cookies.get('averageList'); try { averageList = JSON.parse( averageList ); } catch( error ) { averageList = []; } $scope.averageList = averageList; $scope.rating = 0; $scope.totalPpr = 0; $scope.totalMpr = 0; //add $scope.eventBtnAddNgClick = function() { var aver = new Average(); aver.first = new Result(); aver.second = new Result(); aver.total = new Result(); aver.first.ppr1 = 0; aver.first.mpr = 0; aver.first.ppr2 = 0; aver.second.ppr1 = 0; aver.second.mpr = 0; aver.second.ppr2 = 0; aver.total.ppr1 = 0; aver.total.mpr = 0; $scope.averageList.push( aver ); $scope.eventInputChange(); } //change $scope.eventInputChange = function() { var ppr = 0; var mpr = 0; //Total PPR, MPR angular.forEach( $scope.averageList, function( value, key ) { value.total.ppr1 = ( ( ( value.first.ppr1 + value.first.ppr2 ) / (value.first.ppr2?2:1) ) + ( ( value.second.ppr1 + value.second.ppr2 ) / (value.second.ppr2?2:1) ) ) /2 value.total.mpr = ( value.first.mpr + value.second.mpr ) /2 ppr += value.total.ppr1; mpr += value.total.mpr; } ); $scope.totalPpr = ppr / $scope.averageList.length; $scope.totalMpr = mpr / $scope.averageList.length; //calculate Rating var tempRating = null; GRADE.every( function( value, index, _ary ) { if( value.ppr > $scope.totalPpr || value.mpr > $scope.totalMpr ) { var pprRange = value.ppr - tempRating.ppr; var mprRange = value.mpr - tempRating.mpr; var pprGap = ( $scope.totalPpr - tempRating.ppr ) / pprRange; var mprGap = ( $scope.totalMpr - tempRating.mpr ) / mprRange; $scope.rating = Math.max( tempRating.rating + ( ( pprGap + mprGap ) /2 ), 1 ); return false; } tempRating = value; return true; } ); //save cookie var averageListStr = JSON.stringify( $scope.averageList, function( key, value ) { if( key === "$$hashKey" ) { return undefined; } return value; }) $cookies.put( 'averageList', averageListStr ); } $scope.eventInputChange(); //delete $scope.eventBtnRemoveNgClick = function() { $scope.averageList.splice( $scope.averageList.length -1 ); $cookies.put( 'averageList', JSON.stringify( averageList, function( key, value ) { if( key === "$$hashKey" ) { return undefined; } return value; }) ); $scope.eventInputChange(); } } ] );