Skip to content

Commit 5b5eebc

Browse files
author
Potecaru Tudor
committed
Product Inventary
1 parent 61dfdaf commit 5b5eebc

File tree

2 files changed

+90
-7
lines changed

2 files changed

+90
-7
lines changed
+70
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
/*
2+
Classes 00. Product Inventory Project - Create an application which manages an inventory of products. Create a product class which has a price, id, and quantity on hand.
3+
Then create an inventory class which keeps track of various products and can sum up the inventory value.
4+
*/
5+
6+
(function () {
7+
"use strict";
8+
}());
9+
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+
},
21+
22+
remove : function (count) {
23+
this.quantity = !count ? this.quantity-- : this.quantity - count >= 0 ? this.quantity - count : 0;
24+
},
25+
26+
setPrice : function (value) {
27+
this.price = value;
28+
},
29+
30+
toString : function () {
31+
return this.name;
32+
}
33+
};
34+
35+
function Inventary() {
36+
this.items = arguments !== undefined ? Array.prototype.slice.call(arguments) : [];
37+
}
38+
39+
Inventary.prototype = {
40+
add : function (item) {
41+
this.items.push(item);
42+
},
43+
44+
remove : function (item) {
45+
var index = this.items.indexOf(item);
46+
47+
if (index > -1) {
48+
this.items.splice(index, 1);
49+
}
50+
},
51+
52+
getTotalPrice : function () {
53+
var totalPrice = 0;
54+
55+
return (function (items) {
56+
items.map(function (item) {
57+
totalPrice += item.price;
58+
});
59+
60+
return totalPrice;
61+
}(this.items));
62+
}
63+
};
64+
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);
68+
69+
var inventary = new Inventary(peaches, carrots, bananas);
70+
inventary.getTotalPrice();

Numbers/01. Fibonacci Sequence.js

+20-7
Original file line numberDiff line numberDiff line change
@@ -2,17 +2,30 @@
22
Numbers 01. Fibonacci Sequence - Enter a number and have the program generate the Fibonacci sequence to that number or to the Nth number.
33
*/
44

5-
var fibonacci = function (n) {
5+
var fibonacci = (function () {
66
"use strict";
77

8-
var i,
9-
sequence = [0, 1];
8+
var memo = {};
109

11-
for (i = 2; i < n; i++) {
12-
sequence.push(sequence[i - 2] + sequence[i - 1]);
10+
function f(n) {
11+
var value;
12+
13+
if (memo.hasOwnProperty(n)) {
14+
value = memo[n];
15+
} else {
16+
if (n === 0 || n === 1) {
17+
value = n;
18+
} else {
19+
value = f(n - 1) + f(n - 2);
20+
}
21+
22+
memo[n] = value;
23+
}
24+
25+
return value;
1326
}
1427

15-
return sequence;
16-
};
28+
return f;
29+
}());
1730

1831
this.console.log(fibonacci(10));

0 commit comments

Comments
 (0)