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:
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.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).