5
5
6
6
( function ( ) {
7
7
"use strict" ;
8
- } ( ) ) ;
9
8
10
- function Product ( id , name , price , quantity ) {
11
- this . id = id ;
12
- this . name = name ;
13
- this . price = price ;
14
- this . quantity = quantity ;
15
- }
16
-
17
- Product . prototype = {
18
- add : function ( count ) {
19
- this . quantity = ! count ? this . quantity ++ : this . quantity + count ;
20
- } ,
9
+ function Product ( id , name , price , quantity ) {
10
+ this . id = id ;
11
+ this . name = name ;
12
+ this . price = price ;
13
+ this . quantity = quantity ;
14
+ }
21
15
22
- remove : function ( count ) {
23
- this . quantity = ! count ? this . quantity -- : this . quantity - count >= 0 ? this . quantity - count : 0 ;
24
- } ,
16
+ Product . prototype = {
17
+ add : function ( count ) {
18
+ this . quantity += count === 0 || count ? count : 1 ;
19
+ } ,
25
20
26
- setPrice : function ( value ) {
27
- this . price = value ;
28
- } ,
21
+ remove : function ( count ) {
22
+ this . quantity -= count === 0 || count ? count : 1 ;
23
+ } ,
29
24
30
- toString : function ( ) {
31
- return this . name ;
32
- }
33
- } ;
25
+ setPrice : function ( value ) {
26
+ this . price = value ;
27
+ } ,
34
28
35
- function Inventary ( ) {
36
- this . items = arguments !== undefined ? Array . prototype . slice . call ( arguments ) : [ ] ;
37
- }
29
+ toString : function ( ) {
30
+ return this . name ;
31
+ }
32
+ } ;
38
33
39
- Inventary . prototype = {
40
- add : function ( item ) {
41
- this . items . push ( item ) ;
42
- } ,
34
+ function Inventary ( ) {
35
+ this . items = Array . prototype . slice . call ( arguments ) ;
36
+ }
43
37
44
- remove : function ( item ) {
45
- var index = this . items . indexOf ( item ) ;
38
+ Inventary . prototype = {
39
+ add : function ( item ) {
40
+ this . items . push ( item ) ;
41
+ } ,
46
42
47
- if ( index > - 1 ) {
48
- this . items . splice ( index , 1 ) ;
49
- }
50
- } ,
43
+ remove : function ( item ) {
44
+ var index = this . items . indexOf ( item ) ;
51
45
52
- getTotalPrice : function ( ) {
53
- var totalPrice = 0 ;
46
+ if ( index > - 1 ) {
47
+ this . items . splice ( index , 1 ) ;
48
+ }
49
+ } ,
54
50
55
- return ( function ( items ) {
56
- items . map ( function ( item ) {
57
- totalPrice += item . price ;
58
- } ) ;
51
+ getTotalPrice : function ( ) {
52
+ var sum = this . items . reduce ( function ( previousValue , currentValue , index , array ) {
53
+ return previousValue + currentValue . price ;
54
+ } , 0 ) ;
59
55
60
- return totalPrice ;
61
- } ( this . items ) ) ;
62
- }
63
- } ;
56
+ return sum ;
57
+ }
58
+ } ;
64
59
65
- var peaches = new Product ( 0 , "peaches" , 5 , 5000 ) ;
66
- var carrots = new Product ( 1 , "carrots" , 2 , 10000 ) ;
67
- var bananas = new Product ( 2 , "bananas" , 6 , 3000 ) ;
60
+ var peaches = new Product ( 0 , "peaches" , 5 , 5000 ) ,
61
+ carrots = new Product ( 1 , "carrots" , 2 , 10000 ) ,
62
+ bananas = new Product ( 2 , "bananas" , 6 , 3000 ) ,
63
+ inventary = new Inventary ( peaches , carrots , bananas ) ;
68
64
69
- var inventary = new Inventary ( peaches , carrots , bananas ) ;
70
- inventary . getTotalPrice ( ) ;
65
+ inventary . getTotalPrice ( ) ;
66
+ } ( ) ) ;
0 commit comments