Advertisement
GeorgiLukanov87

Js Front-End - Objects and Classes - Exercises

Feb 26th, 2023 (edited)
577
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. // Objects and Classes - Exercises
  2. // https://judge.softuni.org/Contests/Compete/Index/3792#0
  3.  
  4. // -----------------------------------------------------------------------------------------------------
  5. // 01. Employees
  6.  
  7. function employees(input) {
  8.     for (let person of input) {
  9.         let employeeObj = {
  10.             'name': person,
  11.             'id': person.length,
  12.         }
  13.         console.log(`Name: ${employeeObj.name} -- Personal Number: ${employeeObj.id}`)
  14.     }
  15. }
  16.  
  17. // -----------------------------------------------------------------------------------------------------
  18. // 02. Towns
  19.  
  20. function solve(input) {
  21.     for (let town of input) {
  22.         let details = town.split(' | ');
  23.  
  24.         let townName = details[0];
  25.         let latitude = Number(details[1]).toFixed(2);
  26.         let longitude = Number(details[2]).toFixed(2);
  27.         let townObj = {
  28.             'town': townName,
  29.             'latitude': latitude,
  30.             'longitude': longitude,
  31.         };
  32.  
  33.         console.log(townObj)
  34.     }
  35.  
  36. }
  37.  
  38. // -----------------------------------------------------------------------------------------------------
  39. // 03. Store Provision
  40.  
  41. function solve(product_list1, product_list2) {
  42.     let myObj = {};
  43.  
  44.     function calc_products(product_list) {
  45.         for (let i = 0; i < product_list.length; i += 2) {
  46.             let name = product_list[i];
  47.             let qnty = Number(product_list[i + 1]);
  48.             let result = myObj.hasOwnProperty(name);
  49.             if (!result) {
  50.                 myObj[name] = 0;
  51.             }
  52.             myObj[name] += qnty;
  53.         }
  54.         return myObj
  55.     }
  56.  
  57.     calc_products(product_list1);
  58.     calc_products(product_list2);
  59.  
  60.     for (let [key, value] of Object.entries(myObj)) {
  61.         console.log(`${key} -> ${value}`);
  62.     }
  63. }
  64.  
  65. // -----------------------------------------------------------------------------------------------------
  66. // 04. Movies
  67.  
  68. // 1
  69. function movies(input) {
  70.     let list = [];
  71.     for (const lines of input) {
  72.         if (lines.includes("addMovie")) {
  73.             let nameOfMovie = lines.split("addMovie ")[1];
  74.             list.push({ name:nameOfMovie })
  75.         } else if (lines.includes("directedBy")) {
  76.             let info = lines.split("directedBy ");
  77.             let name = info[0].trim();
  78.             let director = info[1];
  79.             let movie = list.find((movieObj) => movieObj.name === name)
  80.             if (movie) {
  81.                 movie.director = director;
  82.             }
  83.         } else if (lines.includes("onDate")) {
  84.             let info = lines.split("onDate ");
  85.             let name = info[0].trim();
  86.             let date = info[1];
  87.             let movie = list.find((movieObj) => movieObj.name === name);
  88.             if (movie) {
  89.                 movie.date = date;
  90.             }
  91.         }
  92.     }
  93.     for(const movie of list){
  94.         if(movie.name && movie.director && movie.date){
  95.             console.log(JSON.stringify(movie));
  96.         }
  97.     }
  98. }
  99.  
  100. //2
  101.  
  102. function movieDirectors(array) {
  103.     let movies = [];
  104.  
  105.     class Movie {
  106.         constructor(name, director, date) {
  107.             this.name = name;
  108.             this.director = director;
  109.             this.date = date;
  110.         }
  111.     }
  112.     let contains = function (movieName) {
  113.         // let movie = null;
  114.         let movie = movies.find(m => m.name === movieName);
  115.  
  116.         // movies.forEach(m => m.name === movieName ? movie = m : movie = null);
  117.         return movie;
  118.     }
  119.  
  120.     for (let i = 0; i < array.length; i++) {
  121.         let command = array[i].split(' ');
  122.  
  123.         if (command.includes('addMovie')) {
  124.             let name = command.slice(1, command.length).join(' ');
  125.             movies.push(new Movie(name, null, null));
  126.         } else if (command.includes('directedBy')) {
  127.             let name1 = command.slice(0, command.indexOf('directedBy')).join(' ');
  128.             // if (contains(name1) !== null) {
  129.             if (contains(name1) !== undefined) {
  130.                 let movie = contains(name1);
  131.                 movie.director = command.slice(command.indexOf('directedBy') + 1, command.length).join(' ');
  132.             }
  133.         } else if (command.includes('onDate')) {
  134.             let name2 = command.slice(0, command.indexOf('onDate')).join(' ');
  135.             // if (contains(name2) !== null) {
  136.             if (contains(name2) !== undefined) {
  137.                 let movie = contains(name2);
  138.                 movie.date = command.slice(command.indexOf('onDate') + 1, command.length).join(' ');
  139.             }
  140.         }
  141.     }
  142.     movies.forEach(m => m.director != null && m.name != null && m.date != null ? console.log(JSON.stringify(m)) : null);
  143. }
  144.  
  145.  
  146. // -----------------------------------------------------------------------------------------------------
  147. // 05. Inventory
  148.  
  149. function inventory(array) {
  150.     let heroes = [];
  151.  
  152.     class Hero {
  153.         constructor(name,level,items){
  154.             this.name = name,
  155.             this.level = level,
  156.             this.items = items
  157.         }
  158.     }
  159.  
  160.     for(const data of array) {
  161.         let [name, level, items] = data.split(' / ');
  162.         let currentHero = new Hero(name,Number(level),items);
  163.         heroes.push(currentHero);
  164.     }
  165.  
  166.     heroes.sort((a, b) =>
  167.  a.level - b.level
  168.     );
  169.  
  170.     for (const hero of heroes) {
  171.         console.log(`Hero: ${hero.name}\nlevel => ${hero.level}\nitems => ${hero.items}`)
  172.     }
  173. }
  174.  
  175. // -----------------------------------------------------------------------------------------------------
  176. // 06. Word Tracker
  177.  
  178. function solve(text) {
  179.     let objects = {};
  180.     let words = text.shift().split(' ')
  181.  
  182.     for (let word of words) {
  183.         let occurances = 0;
  184.         for (let i = 0; i < text.length; i++) {
  185.             if (text[i] === word)
  186.                 occurances++;
  187.         }
  188.         objects[word] = occurances;
  189.     }
  190.  
  191.     let result = Object.entries(objects).sort((a, b) => b[1] - a[1]);
  192.     result.forEach(element => {
  193.         console.log(`${element[0]} - ${element[1]}`)
  194.     });
  195.  
  196. }
  197.  
  198. // -----------------------------------------------------------------------------------------------------
  199. // 07. Odd Occurrences
  200.  
  201. function solve(text) {
  202.     let result = [];
  203.     let words = text.toLowerCase().split(' ');
  204.  
  205.     for (let word of words) {
  206.         let occurances = 0;
  207.         for (let i = 0; i < words.length; i++) {
  208.             if (words[i] === word)
  209.                 occurances++;
  210.         }
  211.         if (occurances % 2 !== 0 && !result.includes(word)) {
  212.             result.push(word)
  213.         }
  214.  
  215.     }
  216.  
  217.     console.log(...result)
  218.  
  219. }
  220.  
  221. // -----------------------------------------------------------------------------------------------------
  222. // 08. Piccolo
  223.  
  224. function solve(input) {
  225.   let parking = [];
  226.   for (let i = 0; i < input.length; i++) {
  227.     let details = input[i].split(', ')
  228.     let direction = details[0];
  229.     let carNumber = details[1];
  230.  
  231.     if (direction === 'IN' && !parking.includes(carNumber)) {
  232.       parking.push(carNumber);
  233.  
  234.     } else if (direction === 'OUT' && parking.includes(carNumber)) {
  235.       let index = parking.indexOf(carNumber);
  236.       parking.splice(index, 1)
  237.     }
  238.  
  239.   }
  240.   if (parking.length > 0) {
  241.     console.log(parking.sort().join('\n'))
  242.   } else {
  243.     console.log('Parking Lot is Empty')
  244.   }
  245.  
  246. }
  247.  
  248. // -----------------------------------------------------------------------------------------------------
  249. // 09. Make a Dictionary
  250.  
  251. function solve(input) {
  252.     let myDict = {};
  253.     input.forEach(element => {
  254.         let [rawTerm, rawDefinition] = (element.split('":"'))
  255.         let term = rawTerm.slice(2, rawTerm.length)
  256.         let definition = (rawDefinition.replace('"}', ''))
  257.         myDict[term] = definition
  258.     });
  259.  
  260.     for (const [key, value] of Object.entries(myDict).sort()) {
  261.         console.log(`Term: ${key} => Definition: ${value}`)
  262.     }
  263. }
  264.  
  265. // -----------------------------------------------------------------------------------------------------
  266. // 10. Class Vehicle
  267.  
  268. class Vehicle {
  269.     constructor(type, model, parts, fuel) {
  270.         this.type = type;
  271.         this.model = model;
  272.         this.parts = parts;
  273.         this.parts.quality = parts.engine * parts.power;
  274.         this.fuel = fuel;
  275.     }
  276.  
  277.     drive(fuelLoss) {
  278.         this.fuel -= fuelLoss;
  279.     }
  280. }
  281.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement