HTML Academy
Implementing methods
Objects25/30
Back to the list of tasks
  • 1. Shall we play?
  • 2. Players, let’s start!
  • 3. My attempt number one
  • 4. Hello, object!
  • 5. Reading from the object
  • 6. Count off!
  • 7. Overriding object properties
  • 8. Passing object by a link
  • 9. My game
  • 10. Giving out the attempts
  • 11. Who is the winner?
  • 12. Announce the entire list, please
  • 13. Looking for a cat with great results
  • 14. No one will hide
  • 15. New conditions
  • 16. Hard to come across
  • 17. Let’s bring it all to light
  • 18. Roll the dice, gentlemen cats!
  • 19. Let’s make adjustments
  • 20. Summary of “Objects”. Part 1
  • 21. Tenth program: “Golden ball”
  • 22. Build it yourself!
  • 23. My first method
  • 24. Implementing methods
  • 25. Object as a dictionary
  • 26. Bracket notation
  • 27. Let’s not forget about the context
  • 28. Store check
  • 29. Summary of “Objects”. Part 2
  • 30. Eleventh program: “The house that Muffin built”
Bracket notation
  • Sign up
  • Log in

Loading…
Everything will be ready in few seconds

  • Theory
  • Theory
  • Comments

Object as a dictionary

Now let’s calculate the price of the computer. It consists of several components: the starting price and the cost of each technical characteristic. Prices:

Processor i55000
Processor i710000
13″ monitor5000
15″ monitor10000
8 GB memory3000
16 GB memory7000

It turns out that we need to check the property values ​​in the computer object and, depending on the result, add a certain amount to the base price. In the end, we get the price of the device. To test each characteristic separately, you can use the else if structure. Then the code will look like this:

var price = computer.basicPrice;

if (computer.processor === 'i5') {
  price += 5000;
} else if (computer.processor === 'i7') {
  price += 10000;
}

// And so on, summing the cost of the remaining components

The code is working, but isn’t it too long? Now we have only three characteristics that comprise the price, what if there are more of them?

In some situations, you can shorten the condition blocks by using objects. Let’s figure out how we can do it. Let’s look at this using an example.

var printFavoriteFood = function (name) {
  var message = 'My favorite food is ';

  if (name === 'Muffin') {
    message += 'fish';
  } else if (name === 'Rudolph') {
    message += 'cutlet';
  } else if (name === 'Snowball') {
    message += 'milk';
  }

  return message;
};

console.log(printFavoriteFood('Snowball'));
// Logs 'My favorite food is milk'

Note that each cat, or rather the cat’s name, corresponds to a certain meal. And the order of cats has no importance whatsoever. Can I write down the taste preferences of cats in the object? Let’s try.

var catsFavoriteFood = {
  Muffin: 'fish',
  Rudolf: 'cutlet',
  Snowball: 'milk'
};

Can we now use the data in the catsFavoriteFood object so as to completely get rid of the conditions in the body of the function printFavoriteFood? It’s possible! We have your usual object with properties and their values. Which means that we can access the property of the object and add to the expression the line that it contains.

var catsFavoriteFood = {
  Muffin: 'fish',
  Rudolph: 'cutlet',
  Snowball: 'milk'
};

var printFavoriteFood = function (name) {
  return 'My favorite food is ' + catsFavoriteFood[name];
};

console.log(printFavoriteFood ('Snowball'));
// Logs 'My favorite food is milk'

The code works as before, but look at how more concise our program is!

We wrote into the object not a characteristic like name: 'Muffin', but the ratio of the name of the cat and the delicacy, which that particular cat enjoys. Such objects are called dictionaries, maps or associative arrays. They are very convenient to use and allow you to write neater — and therefore, more maintainable — code.

In our program, each indicator has its own price, for example, for the processor i7, it is 10000, for a 13″ display, it is 5000. This means that we can create dictionaries, which will store the value of each characteristic, and then access the properties of these objects to get the price.

Comments

  • script.js
JavaScript
var buildComputer = function (memory, display, processor) { var computer = { basicPrice: 5000, processor: processor, display: display, memory: memory, getDescription: function () { return 'computer with processor ' + computer.processor + ', diagonal ' + computer.display + ', RAM ' + computer.memory; }, getPrice: function () { return computer.basicPrice; } }; return computer; }; var myComputer = buildComputer(8, 13, 'i7'); console.log('In the cart ' + myComputer.getDescription() + ' that costs ' + myComputer.getPrice());

What didn’t you like in this task?

Thanks! We’ll fix everything at once!

Console

The code has changed, click “Run” or turn autorun on.

Result

Goalscompleted
  1. Before the buildComputer function, create object processorPrice with properties 'i5': 5000, 'i7': 10000.
  2. After it, create object displayPrice with properties 13: 5000, 15: 10000.
  3. Add the memoryPrice object with properties 8: 3000, 16: 4000.

Cookies ∙ Privacy ∙ License Agreement ∙ About ∙ Contacts ∙ © HTML Academy OÜ, 2019−2025

VISAMastercard

Log in

or

Forgot your password?

Sign up

Sign up

or
Log in

Restore access

Have you forgotten your password or lost access to your profile? Enter your email connected to your profile and we will send you a link to restore access.

Forgot to connect your email to the profile? Email us and we’ll help.