-
Notifications
You must be signed in to change notification settings - Fork 1
/
script.js
89 lines (65 loc) · 2.45 KB
/
script.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
var app = angular.module('sharesPortfolio', []);
app.controller('stockCtrl', function($scope, $http) {
var price = [];
var stocknum = [];
$scope.stocksArray = [];
//**************** Begin of json data access**********************
$http.get('data.json').then(function(response) {
var indexes = [];
$scope.stocks = response.data.price;
$scope.eps = response.data.eps;
//**************** Begin of adding data to table **********************
$scope.addToTable = function(key, value, index, shares = 1) {
var mystock = {};
mystock.key = key;
mystock.value = value;
mystock.index = index;
mystock.shares = shares;
mystock.eps = $scope.eps[key];
mystock.individualStock = function() {
return this.value * this.shares;
};
if (indexes.indexOf(index) == -1) {
indexes.push(index);
price.push(value);
$scope.stocksArray.push(mystock);
}
};
//****************** Begin of Incrementer and Decrementer*****************
$scope.incrementValue = function(stock) {
$scope.stocksArray[$scope.stocksArray.indexOf(stock)].shares++;
};
$scope.decrementValue = function(stock) {
if (stock.shares < 1) {
stock = 0;
display(stock);
} else {
$scope.stocksArray[$scope.stocksArray.indexOf(stock)].shares--;
}
};
});
// *******************Getting P/E ratio and networth values*****************
$scope.getNetWorth = function() {
var total = 0;
for (var i = 0; i < $scope.stocksArray.length; i++) {
total += $scope.stocksArray[i].individualStock();
// console.log();
}
return total.toFixed(2);
};
$scope.getPortfolioPE = function() {
var temp = 0;
var total = 0;
for (var i = 0; i < $scope.stocksArray.length; i++) {
total += $scope.stocksArray[i].eps * $scope.stocksArray[i].shares;
}
if (total === 0) {
total = 1;
}
temp += $scope.getNetWorth() / total;
return temp.toFixed(2);
};
$scope.remove = function(idx) {
$scope.stocksArray.splice(idx, 1); //Delete Stocks
};
});