Develop the HashStorage class (in the HashStorage.js file) for storing arbitrary key-value pairs. The key can be any string; A value can be of any type, including a complex one (an object, an array, or a function).

The class must have the following interface (that is, have the following public methods):
● addValue (key, value) - saves the specified value under the specified key;
● getValue (key) - returns the value at the specified key or undefined;
● deleteValue (key) - deletes the value with the specified key, returns true if the value was deleted and false if no such value was in the repository;
● getKeys () - returns an array consisting of only keys.

The class should not use any global variables; it should not β€œsmudge the screen”. The class should be universal, i.e. do not depend on the structure of the stored data, nor on the method of their subsequent use (including it should not contain any references to the DOM, as it can be used without a web page at all).

Create a HashStorage class coctailsStorage descendant object for storing cocktail recipes. The key is the name of the drink, and its value is information about the drink (alcoholic or not, a string with ingredients, a recipe, etc. (optional)).

On the page to lay out the buttons:
● β€œrecipe entry” - sequentially asks the name of the drink, whether it is alcoholic or not, what ingredients are necessary and in what proportions, the recipe for its preparation (etc.); stores all this information about the cocktail in the vault (coctailsStorage).
● β€œdrink recipe” - asks for the name of the drink and issues information about it to the page (preferably) or to the console using the example below, or a message about the absence of such a drink in the storage.
● β€œrecipe deletion” - asks for the name of the drink and removes it from the storage (if there is one) with the issuance of the appropriate message to the user.
● β€œlist of all cocktails” - displays a list of names of cocktails from the storage.

Examples of the output / design of the recipe:

  1. Cocktail "Margarita" (alcohol: yes)
    Ingredients Required:
    Vodka Finlandia 50ml
    Coffee liqueur 25ml
    Ice cubes 120 g
    recipe:
    Fill a glass with ice cubes to the top, then pour the coffee liqueur
    25 ml, vodka 50 ml and stir with a cocktail spoon.

  2. Cocktail "Pelican" (alcohol: no)
    Ingredients Required:
    Grenadine Monin 10ml
    Strawberry syrup Monin 10ml
    Peach Juice 150ml
    Lemon Juice 15ml
    Banana 110g
    Strawberry 50g
    Crushed ice 60g
    Recipe:
    Put in a blender the peeled and chopped half a banana and
    strawberries 2 berries. Pour lemon juice 15 ml, grenadine 10 ml,
    strawberry syrup 10 ml and peach juice 150 ml. Add to blender
    a scoop of crushed ice and whisk. Pouring into a highball. Circle
    banana and half a strawberry on a cocktail skewer.

My efforts:

function HashStorage(){ //Π΄ΠΎΠ±Π°Π²Π»Π΅Π½ΠΈΠ΅ this.add = function addValue(key,value){ //ΠΏΡƒΠ±Π»ΠΈΡ‡Π½Ρ‹ΠΉ ΠΌΠ΅Ρ‚ΠΎΠ΄ ΠΊΠ°ΠΊ элСмСнт Ρ…ΡΡˆΠ° this key=prompt('НазваниС коктСйля: '); coctailsStorage.key = {}; coctailsStorage.key.alcoholic = prompt('ΠΠ»ΠΊΠΎΠ³ΠΎΠ»ΡŒΠ½Ρ‹ΠΉ Π»ΠΈ?'); coctailsStorage.key.ingredients = prompt('Π˜Π½Π³Ρ€Π΅Π΄ΠΈΠ΅Π½Ρ‚Ρ‹ ΠΈ ΠΈΡ… ΠΏΡ€ΠΎΠΏΠΎΡ€Ρ†ΠΈΠΈ:'); coctailsStorage.key.recipe = prompt('Π Π΅Ρ†Π΅ΠΏΡ‚ приготовлСния:'); } //ΠΏΠΎΠ»ΡƒΡ‡Π΅Π½ΠΈΠ΅ this.get = function getValue(key){ key=prompt('НазваниС коктСйля: '); if(key in coctailsStorage){ alert('ΠšΠΎΠΊΡ‚Π΅ΠΉΠ»ΡŒ'+key+'(Π°Π»ΠΊΠΎΠ³ΠΎΠ»ΡŒΠ½Ρ‹ΠΉ'+coctailsStorage.key.alcoholic+')\n'+ 'НСобходимыС ΠΈΠ½Π³Ρ€Π΅Π΄ΠΈΠ΅Π½Ρ‚Ρ‹:\n'+coctailsStorage.key.ingredients+'\n Ρ€Π΅Ρ†Π΅ΠΏΡ‚ приготовлСния:\n'+coctailsStorage.key.recipe); } else{ alert('Π’Π°ΠΊΠΎΠ³ΠΎ коктСйля Π² спискС Π½Π΅Ρ‚'); } } //ΡƒΠ΄Π°Π»Π΅Π½ΠΈΠ΅ this.delete = function deleteValue(key){ key=prompt('НазваниС коктСйля: '); if(key in coctailsStorage){ delete coctailsStorage.key; alert('Π£Π΄Π°Π»Π΅Π½'); console.log('true'); } else{ alert('Π’Π°ΠΊΠΎΠ³ΠΎ коктСйля Π² спискС Π½Π΅Ρ‚'); console.log('false'); } } //ΠΏΠ΅Ρ€Π΅Ρ‡Π΅Π½ΡŒ ΠΊΠ»ΡŽΡ‡Π΅ΠΉ this.onlyKeys = function getKeys(){ for(key in coctailsStorage){ console.log(key) } } const coctailsStorage = new HashStorage(); coctailsStorage = {'ΠœΠ°Ρ€Π³Π°Ρ€ΠΈΡ‚Π°': {alcoholic:'Π΄Π°', ingredients:'Π’ΠΎΠ΄ΠΊΠ° Finlandia 50ΠΌΠ»\nΠšΠΎΡ„Π΅ΠΉΠ½Ρ‹ΠΉ Π»ΠΈΠΊΠ΅Ρ€ 25ΠΌΠ»\n' + 'Π›Π΅Π΄ Π² ΠΊΡƒΠ±ΠΈΠΊΠ°Ρ… 120 Π³', recipe:'Наполни стакан ΠΊΡƒΠ±ΠΈΠΊΠ°ΠΌΠΈ льда Π΄ΠΎΠ²Π΅Ρ€Ρ…Ρƒ, Π·Π°Ρ‚Π΅ΠΌ Π½Π°Π»Π΅ΠΉ ΠΊΠΎΡ„Π΅ΠΉΠ½Ρ‹ΠΉ Π»ΠΈΠΊΠ΅Ρ€\\n\' +\n' + ' \'25 ΠΌΠ», Π²ΠΎΠ΄ΠΊΡƒ 50 ΠΌΠ» ΠΈ Ρ€Π°Π·ΠΌΠ΅ΡˆΠ°ΠΉ ΠΊΠΎΠΊΡ‚Π΅ΠΉΠ»ΡŒΠ½ΠΎΠΉ Π»ΠΎΠΆΠΊΠΎΠΉ.'}, 'ПСликан':{alcoholic:'Π½Π΅Ρ‚', ingredients:'Π“Ρ€Π΅Π½Π°Π΄ΠΈΠ½ Monin 10ΠΌΠ»\n' + 'ΠšΠ»ΡƒΠ±Π½ΠΈΡ‡Π½Ρ‹ΠΉ сироп Monin 10ΠΌΠ»\n' + 'ΠŸΠ΅Ρ€ΡΠΈΠΊΠΎΠ²Ρ‹ΠΉ сок 150ΠΌΠ»\n' + 'Π›ΠΈΠΌΠΎΠ½Π½Ρ‹ΠΉ сок 15ΠΌΠ»\n' + 'Π‘Π°Π½Π°Π½ 110Π³\n' + 'ΠšΠ»ΡƒΠ±Π½ΠΈΠΊΠ° 50Π³\n' + 'Π”Ρ€ΠΎΠ±Π»Π΅Π½Ρ‹ΠΉ Π»Π΅Π΄ 60Π³', recipe:'ПолоТи Π² Π±Π»Π΅Π½Π΄Π΅Ρ€ ΠΎΡ‡ΠΈΡ‰Π΅Π½Π½ΡƒΡŽ ΠΈ Π½Π°Ρ€Π΅Π·Π°Π½Π½ΡƒΡŽ ΠΏΠΎΠ»ΠΎΠ²ΠΈΠ½ΠΊΡƒ Π±Π°Π½Π°Π½Π° ΠΈ \n' + 'ΠΊΠ»ΡƒΠ±Π½ΠΈΠΊΡƒ 2 ягоды. НалСй Π»ΠΈΠΌΠΎΠ½Π½Ρ‹ΠΉ сок 15 ΠΌΠ», Π³Ρ€Π΅Π½Π°Π΄ΠΈΠ½ 10 ΠΌΠ»,\n'+ 'ΠΊΠ»ΡƒΠ±Π½ΠΈΡ‡Π½Ρ‹ΠΉ сироп 10 ΠΌΠ» ΠΈ пСрсиковый сок 150 ΠΌΠ». Π”ΠΎΠ±Π°Π²ΡŒ Π² Π±Π»Π΅Π½Π΄Π΅Ρ€\n'+ 'совок Π΄Ρ€ΠΎΠ±Π»Π΅Π½ΠΎΠ³ΠΎ льда ΠΈ Π²Π·Π±Π΅ΠΉ. ΠŸΠ΅Ρ€Π΅Π»Π΅ΠΉ Π² Ρ…Π°ΠΉΠ±ΠΎΠ». Π£ΠΊΡ€Π°ΡΡŒ ΠΊΡ€ΡƒΠΆΠΊΠΎΠΌ\n'+ 'Π±Π°Π½Π°Π½Π° ΠΈ ΠΏΠΎΠ»ΠΎΠ²ΠΈΠ½ΠΊΠΎΠΉ ΠΊΠ»ΡƒΠ±Π½ΠΈΠΊΠΈ Π½Π° ΠΊΠΎΠΊΡ‚Π΅ΠΉΠ»ΡŒΠ½ΠΎΠΉ шпаТкС.'}}; } 

I seem to be completely confused. I am particularly confused by the phrase from the assignment β€œshould not contain any references to the DOM, since can be used without a web page at all. ” I can not understand how to properly attach buttons to functions. Used onclick = 'this.add ()' handler, but does not exit. I still do not understand how to write the values ​​specifically in coctailsStorage (repository).

  • And exactly the whole code? And if it breaks off in the middle (the curly bracket is at least not enough) - ThisMan
  • Isn't it easier then to really write on classes? - And
  • It seems like the whole. Maybe I just did not think that I need to write something else. And braces ... I checked if everything was in order with them and somehow I did not find a mistake. WS does not swear - Nastya

1 answer 1

Judging by the code you have a mess in your head, or the code is not clear where.

Right now there will be a lot of text, but if not very interesting, then the answer is at the very end
Let's start with the fact that the class methods need to be written to the prototype, otherwise each instance will have its own method, which, with a large number of instances, will β€œeat” a lot of extra memory

 this.add = function() {}; // Π½Π΅ ΠΎΡ‡Π΅Π½ΡŒ Ρ…ΠΎΡ€ΠΎΡˆΠΎ HashStorage.prototype.add = function () {}; // ΡƒΠΆΠ΅ Π»ΡƒΡ‡ΡˆΠ΅ 

Secondly: why did you put the creation of the instance into the designer itself, and besides, you are not using it correctly

 const coctailsStorage = new HashStorage(); // ΠΌΠΎΠΆΠ΅ΠΌ ΠΏΠΎΠ»ΡŒΠ·ΠΎΠ²Π°Ρ‚ΡŒΡΡ ΠΌΠ΅Ρ‚ΠΎΠ΄Π°ΠΌΠΈ coctailsStorage = { ... }; // взяли ΠΈ пСрСзаписали ΠΏΠ΅Ρ€Π΅ΠΌΠ΅Π½Π½ΡƒΡŽ, ΠΌΠ΅Ρ‚ΠΎΠ΄Ρ‹ Π½Π΅ доступны большС 

The structure of your class itself is not correct, since an external variable is used for storage, which in fact should not have anything to do with the implementation. Plus, again, it is not used correctly, because you are again overwriting a completely variable and it seems there will be even a syntax error, since you are trying to access const before it is declared.

The essence of the task was to create a class that can be reused, the phrase

including should not contain any references to the DOM, since can be used without any webpage at all

means that the code can be run in another environment where you can execute js , but where there is no browser api and DOM , for example nodejs

As a result, we will write the HashStorage class, we will use the actual syntax using the classes

 class HashStorage { constructor() { this.store_ = {}; // Ρ…Ρ€Π°Π½ΠΈΠ»ΠΈΡ‰Π΅ для Π·Π½Π°Ρ‡Π΅Π½ΠΈΠΉ // ΠΏΠΎΠ΄Ρ‡Π΅Ρ€ΠΊΠΈΠ²Π°Π½ΠΈΠ΅ Π² ΠΊΠΎΠ½Ρ†Π΅ ΠΏΠ΅Ρ€Π΅ΠΌΠ΅Π½Π½ΠΎΠΉ Π³ΠΎΠ²ΠΎΡ€ΠΈΡ‚ ΠΎ Ρ‚ΠΎΠΌ, Ρ‡Ρ‚ΠΎ ΠΎΠ½Π° приватная // Ρ‚Π°ΠΊ Ρ‡Ρ‚ΠΎ Π½Π° ΠΏΡ€ΡΠΌΡƒΡŽ Π΅Π΅ ΠΈΡΠΏΠΎΠ»ΡŒΠ·ΠΎΠ²Π°Ρ‚ΡŒ нСльзя // Π½ΠΎ ΠΏΠΎΠΊΠ° Ρ‡Ρ‚ΠΎ это Ρ‚ΠΎΠ»ΡŒΠΊΠΎ соглашСниС } add(key, value) { this.store_[key] = value; // сохраняСм Π·Π½Π°Ρ‡Π΅Π½ΠΈΠ΅ Π² store } getValue(key) { return this.store_[key]; // Ρ‚ΡƒΡ‚ всС Ρ‚ΠΎΠΆΠ΅ просто ΠΈ Π½Π°Ρ‚ΠΈΠ²Π½ΠΎ } deleteValue(key) { if(!(key in this.store_)) { return false } return delete this.store_[key]; } getKeys() { return Object.keys(this.store_); } } // Π’Π΅ΠΏΠ΅Ρ€ΡŒ coctailsStorage ΠΌΠΎΠΆΠ΅Ρ‚ ΠΈΡΠΏΠΎΠ»ΡŒΠ·ΠΎΠ²Π°Ρ‚ΡŒ ΠΌΠ΅Ρ‚ΠΎΠ΄Ρ‹ // add, getValue etc. // Плюс экзСмпляр ΠΈΠΌΠ΅Π΅Ρ‚ своС собствСнноС Ρ…Ρ€Π°Π½ΠΈΠ»ΠΈΡ‰Π΅ const coctailsStorage = new HashStorage(); coctailsStorage.add('vodka', {alcohol: true}); // добавляСм Π·Π½Π°Ρ‡Π΅Π½ΠΈΠ΅ coctailsStorage.add('tea', {alcohol: false}); // добавляСм Π·Π½Π°Ρ‡Π΅Π½ΠΈΠ΅ console.log(coctailsStorage.getValue('vodka')); console.log(coctailsStorage.getKeys()); console.log(coctailsStorage.deleteValue('vodka')); console.log(coctailsStorage.getValue('vodka')); console.log(coctailsStorage.getKeys()); 

As for the buttons, you just need to create an interlayer that will take the buttons, an instance of coctailsStorage and combine them, it may not even be a class, but just something related, for example, a set of functions. I will give an example for the add button, the rest you write yourself

 class HashStorage { constructor() { this.store_ = {}; // Ρ…Ρ€Π°Π½ΠΈΠ»ΠΈΡ‰Π΅ для Π·Π½Π°Ρ‡Π΅Π½ΠΈΠΉ } add(key, value) { this.store_[key] = value; // сохраняСм Π·Π½Π°Ρ‡Π΅Π½ΠΈΠ΅ Π² store } getValue(key) { return this.store_[key]; // Ρ‚ΡƒΡ‚ всС Ρ‚ΠΎΠΆΠ΅ просто ΠΈ Π½Π°Ρ‚ΠΈΠ²Π½ΠΎ } deleteValue(key) { if(!(key in this.store_)) { return false } return delete this.store_[key]; } getKeys() { return Object.keys(this.store_); } } const coctailsStorage = new HashStorage(); // Π”Π°Π»ΡŒΡˆΠ΅ Π±ΡƒΠ΄Ρƒ ΠΎΠΏΠΈΡΡ‹Π²Π°Ρ‚ΡŒΡΡ ΠΊΠ½ΠΎΠΏΠΊΠΈ const addButton = document.getElementById('add-coctail'); addButton.onclick = function () { const coctail = {}; // ΠΏΠΎΠ»ΡƒΡ‡Π°Π΅ΠΌ Π΄Π°Π½Π½Ρ‹Π΅ ΠΎΡ‚ ΠΏΠΎΠ»ΡŒΠ·ΠΎΠ²Π°Ρ‚Π΅Π» const name = window.prompt('Π’Π²Π΅Π΄ΠΈΡ‚Π΅ Π½Π°Π·Π²Π°Π½ΠΈΠ΅ коктСля'); const isAlcohol = window.prompt('Напиток Π°Π»ΠΊΠΎΠ³ΠΎΠ»ΡŒΠ½Ρ‹ΠΉ?'); // ДобавляСм Π·Π½Π°Ρ‡Π΅Π½ΠΈΠ΅ Π² нашС Ρ…Ρ€Π°Π½ΠΈΠ»ΠΈΡ‰Π΅ coctailsStorage.add(name, {name, isAlcohol}); console.log('Coctail was added', coctailsStorage.getValue(name)); } 
 <button id='add-coctail'>Π”ΠΎΠ±Π°Π²ΠΈΡ‚ΡŒ</button> <button id='delete-coctail'>Π£Π΄Π°Π»ΠΈΡ‚ΡŒ</button> <button id='get-coctail'>Π Π΅Ρ†Π΅ΠΏΡ‚</button> <button id='all-coctails'>Бписок Ρ€Π΅Ρ†Π΅ΠΏΡ‚ΠΎΠ²</button>