Advertisement
divanov94

Untitled

Aug 9th, 2020
54
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. function plantDiscovery(input){
  2.     let countPlants=Number(input.shift());
  3.     let obj={};
  4.     let plants=input.splice(0,countPlants);
  5.  
  6.     for (let plant of plants){
  7.         let [name,rarity]=plant.split("<->");
  8.         rarity=Number(rarity);
  9.  
  10.         if(!obj.hasOwnProperty(name)){
  11.             obj[name]= {
  12.                 rarity,
  13.                 rating:[]
  14.  
  15.             }
  16.         }
  17.     }
  18.    let commands=input.splice(0,input.indexOf("Exhibition"));
  19.    for(let line of commands){
  20.        let [command,lineOth]=line.split(": ");
  21.        let [plantName,value]=lineOth.split(" - ");
  22.        value=Number(value);
  23.        let avg=0;
  24.  
  25.  
  26.        switch(command){
  27.            case "Rate":
  28.                if(!obj.hasOwnProperty(plantName)){
  29.                    console.log(`error`);
  30.  
  31.                }else {
  32.                    obj[plantName].rating.push(value);
  33.  
  34.  
  35.  
  36.                }
  37.  
  38.  
  39.                break;
  40.            case "Update":
  41.                if(!obj.hasOwnProperty(plantName)){
  42.                    console.log(`error`);
  43.                }else {
  44.                   obj[plantName].rarity=value;
  45.  
  46.                }
  47.                break;
  48.            case "Reset":
  49.                if(!obj.hasOwnProperty(plantName)){
  50.                    console.log(`error`);
  51.                }else {
  52.                    delete obj[plantName].rating;
  53.                }
  54.                break;
  55.  
  56.        }
  57.    }
  58.     //console.log(obj)
  59.  
  60. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement