-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcashAmount.js
executable file
·79 lines (64 loc) · 2.57 KB
/
cashAmount.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
var CashAmount = function(money) {
this.money = money; //number
this.moneyStr = this.money.toFixed(2); //string
this.integer = Number(this.moneyStr.split('.')[0]);
this.decimal = Number(this.moneyStr.split('.')[1]);
}
CashAmount.prototype.totalInPennies = function() {
return this.money * 100;
}
CashAmount.prototype.addDoubleAmount = function(addMoney) {
this.addMoneyStr = addMoney.toFixed(2);
this.addInteger = Number(this.addMoneyStr.split('.')[0]);
this.addDecimal = Number(this.addMoneyStr.split('.')[1]);
var tempDecimal = this.addDecimal + this.decimal;
if (tempDecimal >= 100) {
var totalDecimal = (tempDecimal / 100).toFixed(2).split('.')[1];
var moreInteger = (tempDecimal / 100).toFixed(2).split('.')[0];
var totalInteger = this.addInteger + this.integer + Number(moreInteger);
this.money = Number(([totalInteger, totalDecimal]).join('.'));
this.moneyStr = this.money.toFixed(2); //string
this.integer = Number(this.moneyStr.split('.')[0]);
this.decimal = Number(this.moneyStr.split('.')[1]);
} else {
var totalDecimal = tempDecimal;
var totalInteger = this.addInteger + this.integer;
this.money = Number(([totalInteger, totalDecimal]).join('.'));
this.moneyStr = this.money.toFixed(2); //string
this.integer = Number(this.moneyStr.split('.')[0]);
this.decimal = Number(this.moneyStr.split('.')[1]);
}
}
CashAmount.prototype.quantityOfEachDenomination = function() {
var quantity = {};
quantity.hundreds = Math.floor(this.integer / 100);
quantity.fifties = Math.floor((this.integer % 100) / 50);
quantity.twenties = Math.floor(((this.integer % 100) % 50 ) / 20);
quantity.tens = Math.floor((((this.integer % 100) % 50) % 20) / 10);
quantity.fives = Math.floor(((((this.integer % 100) % 50) % 20) %10) / 5);
quantity.ones = ((((this.integer % 100) % 50) % 20) %10) % 5;
quantity.quarters = Math.floor(this.decimal / 25);
quantity.dimes = Math.floor((this.decimal % 25) / 10);
quantity.nickels = Math.floor(((this.decimal % 25) %10) / 5);
quantity.penny = ((this.decimal % 25) %10) % 5;
return quantity;
}
var test = new CashAmount(123.72);
var result = Object.getPrototypeOf(test);
console.log(result);
CashAmount.prototype.toDouble = function() {
}
CashAmount.prototype.toDoubleString = function() {
}
// class TestSuite {
// runTests() {
// // your code here
// }
//
// testFoo() { /* some test code */ }
// testBar() { /* some test code */ }
// testBaz() { /* some test code */ }
// }
//
// const suite = new TestSuite();
// suite.runTests(); // testFoo, testBar, and testBaz are executed