Untitled
unknown
javascript
3 years ago
5.1 kB
1
Indexable
'use strict' /** * Reflection question 1 */ const imported = require('./inventory.js') // console.log(imported.inventory['Sallad']) // let names = Object.keys(imported.inventory) // names.forEach(name => console.log(name)) console.log('Object.keys():') let names = Object.keys(imported.inventory) names .sort((a, b) => a.localeCompare(b, 'sv', { sensitivity: 'case' })) .forEach(name => console.log(name)) /** * Reflection question 2 */ console.log('\n--- Assignment 1 ---------------------------------------') const makeOptions = (inventory, filter) => { let names = Object.keys(inventory) return names .filter(name => inventory[name][filter]) .map( name => `<option value="${name}"> ${name}, ${inventory[name].price} kr</option>` ) .join('\n') } console.log(makeOptions(imported.inventory, 'foundation')) console.log('\n--- Assignment 2 ---------------------------------------') class Salad { static counter = 0 constructor() { const _uuid = `salad_${Salad.counter++}` this.uuid = function () { return _uuid } this.ingredients = {} } add(name, properties) { this.ingredients[name] = properties return this } remove(name) { delete this.ingredients[name] return this } // getPrice() { // return Object.keys(this.ingredients) // .map(name => this.ingredients[name].price) // .reduce((a, b) => a + b) // } // count(component) { // return Object.keys(this.ingredients).filter( // name => this.ingredients[name][component] // ).length // } } let myCaesarSalad = new Salad() .add('Sallad', imported.inventory['Sallad']) .add('Kycklingfilé', imported.inventory['Kycklingfilé']) .add('Bacon', imported.inventory['Bacon']) .add('Krutonger', imported.inventory['Krutonger']) .add('Parmesan', imported.inventory['Parmesan']) .add('Ceasardressing', imported.inventory['Ceasardressing']) .add('Gurka', imported.inventory['Gurka']) console.log(JSON.stringify(myCaesarSalad) + '\n') myCaesarSalad.remove('Gurka') console.log(JSON.stringify(myCaesarSalad) + '\n') console.log('\n--- Assignment 3 ---------------------------------------') Salad.prototype.getPrice = function () { return Object.keys(this.ingredients) .map(name => this.ingredients[name].price) .reduce((a, b) => a + b) } Salad.prototype.count = function (component) { return Object.keys(this.ingredients).filter( name => this.ingredients[name][component] ).length } console.log('En ceasarsallad kostar ' + myCaesarSalad.getPrice() + 'kr') // En ceasarsallad kostar 45kr console.log( 'En ceasarsallad har ' + myCaesarSalad.count('extra') + ' tillbehör' ) // En ceasarsallad har 3 tillbehör // reflection question 3 console.log('typeof Salad: ' + typeof Salad) console.log('typeof Salad.prototype: ' + typeof Salad.prototype) console.log( 'typeof Salad.prototype.prototype: ' + typeof Salad.prototype.prototype ) console.log('typeof myCaesarSalad: ' + typeof myCaesarSalad) console.log('typeof myCaesarSalad.prototype: ' + typeof myCaesarSalad.prototype) console.log( 'check 1: ' + (Salad.prototype === Object.getPrototypeOf(myCaesarSalad)) ) console.log( 'check 2: ' + (Object.prototype === Object.getPrototypeOf(Salad.prototype)) ) console.log('\n--- Assignment 4 ---------------------------------------') class GourmetSalad extends Salad { add(name, properties, size) { if (size) { const newSize = this.ingredients[name] ? this.ingredients[name].size + size : size const price = properties.price * newSize super.add(name, { ...properties, size: newSize, price, }) } else { super.add(name, properties) } return this } remove(name) { super.remove(name) return this } } let myGourmetSalad = new GourmetSalad() .add('Sallad', imported.inventory['Sallad'], 0.5) .add('Kycklingfilé', imported.inventory['Kycklingfilé'], 2) .add('Bacon', imported.inventory['Bacon'], 0.5) .add('Krutonger', imported.inventory['Krutonger']) .add('Parmesan', imported.inventory['Parmesan'], 2) .add('Ceasardressing', imported.inventory['Ceasardressing']) console.log( 'Min gourmetsallad med lite bacon kostar ' + myGourmetSalad.getPrice() + ' kr' ) // console.log(myGourmetSalad) myGourmetSalad.add('Bacon', imported.inventory['Bacon'], 1) console.log('Med extra bacon kostar den ' + myGourmetSalad.getPrice() + ' kr') console.log('\n--- Assignment 5 ---------------------------------------') Object.defineProperty(myGourmetSalad, 'quantity', { get() { return 1 }, set(v) {}, }) console.log('Min gourmetsallad har uuid: ' + myGourmetSalad.uuid()) console.log('myGourmetSalad.quantity: ' + myGourmetSalad.quantity) myGourmetSalad.quantity = 4 console.log( 'myGourmetSalad.quantity (after change): ' + myGourmetSalad.quantity ) console.log(`Salad.counter: ${Salad.counter}`) console.log(`Salad.prototype.counter: ${Salad.prototype.counter}`) /** * Reflection question 4 */ /** * Reflection question 5 */ /** * Reflection question 6 */
Editor is loading...