Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // IMBD MOVIES
- // var popular = newReleases.filter(a=>a.rating===5.0).map(a=>a.id); get all movies with raiting >= 5.0
- Singleton = function()
- {
- let instance;
- return {
- getInstance: function ()
- {
- if (!instance)
- instance = Date.now();
- return instance;
- }
- }
- }();
- // ***************************************************
- // Shopping Cart functions
- function shoppingCart(){
- this.items = []
- }
- shoppingCart.prototype.addItemToCart = function(name, price, count){
- for(var i=0; i<this.items.length; i++)
- {
- if(this.items[i].name == name)
- {
- this.items[i].count += count;
- return;
- }
- }
- this.items.push({
- name: name,
- price: price,
- count: count
- })
- }
- shoppingCart.prototype.setCountForItem = function(name, count){
- if(count > 0)
- for(var i=0; i<this.items.length;i++)
- {
- if(this.items[i].name == name)
- {
- this.items[i].count = count;
- break;
- }
- }
- else {
- this.removeItemFromCart(name);
- }
- }
- shoppingCart.prototype.removeItemFromCartAll = function(name){
- this.items = this.items.filter(el => {
- return el.name != name
- });
- }
- shoppingCart.prototype.clearCart = function(){
- this.items = []
- }
- shoppingCart.prototype.countCart = function(){
- return this.items.map(el => el.count).reduce((acc, el) => acc + el, 0);
- }
- shoppingCart.prototype.totalCart = function() {
- return this.items.reduce((acc, el) =>{
- acc += el.price * el.count;
- return acc;
- }, 0);
- }
- shoppingCart.prototype.listCart = function() {
- return this.items.map(el => {
- return {
- ...el,
- total: el.price * el.count
- }
- })
- }
- var shoppingCart = new shoppingCart();
- console.log("Testing Singleton!");
- console.log("Creating i1");
- console.log("i1 was created at time [Number: " + Singleton.getInstance() + "]");
- console.log("Creating i2 at time: " + Date.now());
- console.log("i2 was created with time [Number: " + Singleton.getInstance() + "]");
- let first = Singleton.getInstance() === Singleton.getInstance();
- console.log("Checking if i1 and i2 are the same instance: " + first);
- console.log("Creating i3 in different execution context at time: " + Date.now());
- console.log("i3 was created with time [NUmber: " + Singleton.getInstance() + "]");
- let second = Singleton.getInstance() === Singleton.getInstance();
- let third = Singleton.getInstance() === Singleton.getInstance();
- let fourth = Singleton.getInstance() === Singleton.getInstance();
- console.log("Checking if i1 and i3 are the same variable: " + second);
- console.log("Checking if i2 and i3 are the same variable: " + third);
- console.log("Checking if i1 and i2 are the same variable: " + second);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement