var INGRIDIENTS = {
'cheese': {
weight: 50,
calories: 50,
cost: 0.5
},
'sauce': {
weight: 10,
calories: 20,
cost: 20
},
'mozzarela': {
weight: 25,
calories: 25,
cost: 0.5
},
'peperoni': {
weight: 20,
calories: 20,
cost: 0.3
}
}
function Pizza(recipe) {
// правила создания объекта - его инициализация
// this который доступен в этой функции - это новый объект, который будет создан если функцию вызвать с new
this.base = recipe.base
this.ingridients = recipe.ingridients
this.cost = recipe.cost
}
Pizza.prototype = { // or Object.create
getWeight: function() { // 'эта функция называется метод объекта
// this такой же как в функции-конструкторе
// мы можем из него брать любые свойства
var ingridientWeights = this.ingridients
.map(function(el) {
return el.weight
})
var allIngridientsWeight = ingridientWeights.reduce(function(sum, cur) {
return sum + cur
}, this.base.weight)
return allIngridientsWeight
},
getCalories: function() {
var ingridientCalories = this.ingridients
.map(function(el) {
return el.calories
})
var allIngridientsCalories = ingridientCalories.reduce(function(sum, cur) {
return sum + cur
}, this.base.calories)
return allIngridientsCalories
},
//посчитать цену и скидку на пиццу, на каждый ингридиент, если сумма заказа более 30 - скидка 10%, если вес пиццы более 100 - скидка +5%
/* getSale: function() {
} модифицировать, упрастить можно*/
getCost: function() {
var ingridientCost = this.ingridients
.map(function(el) {
return el.cost
})
var allIngridientsCost = ingridientCost.reduce(function(sum, cur) {
return sum + cur
}, this.base.cost)
if (allIngridientsCost >= 30){
console.log('cost', allIngridientsCost)
console.log('sale 10%:', allIngridientsCost*0.1)
newCost =...
Please Whitelist JSFiddle in your content blocker.
Help keep JSFiddle free for always by one of two ways:
Whitelist JSFiddle in your content blocker (two clicks)
Go PRO and get access to additional PRO features →
Join the 4+ million users, and keep the JSFiddle dream alive.
Ad-free
All ads in the editor and listing pages are turned completely off.
Use pre-released features
You get to try and use features (like the Palette Color Generator) months before everyone else.
Fiddle collections
Sort and categorize your Fiddles into multiple collections.
Private collections and fiddles
You can make as many Private Fiddles, and Private Collections as you wish!
Console
Debug your Fiddle with a minimal built-in JavaScript console.