Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // Objects and Classes - Exercises
- // https://judge.softuni.org/Contests/Compete/Index/3792#0
- // -----------------------------------------------------------------------------------------------------
- // 01. Employees
- function employees(input) {
- for (let person of input) {
- let employeeObj = {
- 'name': person,
- 'id': person.length,
- }
- console.log(`Name: ${employeeObj.name} -- Personal Number: ${employeeObj.id}`)
- }
- }
- // -----------------------------------------------------------------------------------------------------
- // 02. Towns
- function solve(input) {
- for (let town of input) {
- let details = town.split(' | ');
- let townName = details[0];
- let latitude = Number(details[1]).toFixed(2);
- let longitude = Number(details[2]).toFixed(2);
- let townObj = {
- 'town': townName,
- 'latitude': latitude,
- 'longitude': longitude,
- };
- console.log(townObj)
- }
- }
- // -----------------------------------------------------------------------------------------------------
- // 03. Store Provision
- function solve(product_list1, product_list2) {
- let myObj = {};
- function calc_products(product_list) {
- for (let i = 0; i < product_list.length; i += 2) {
- let name = product_list[i];
- let qnty = Number(product_list[i + 1]);
- let result = myObj.hasOwnProperty(name);
- if (!result) {
- myObj[name] = 0;
- }
- myObj[name] += qnty;
- }
- return myObj
- }
- calc_products(product_list1);
- calc_products(product_list2);
- for (let [key, value] of Object.entries(myObj)) {
- console.log(`${key} -> ${value}`);
- }
- }
- // -----------------------------------------------------------------------------------------------------
- // 04. Movies
- // 1
- function movies(input) {
- let list = [];
- for (const lines of input) {
- if (lines.includes("addMovie")) {
- let nameOfMovie = lines.split("addMovie ")[1];
- list.push({ name:nameOfMovie })
- } else if (lines.includes("directedBy")) {
- let info = lines.split("directedBy ");
- let name = info[0].trim();
- let director = info[1];
- let movie = list.find((movieObj) => movieObj.name === name)
- if (movie) {
- movie.director = director;
- }
- } else if (lines.includes("onDate")) {
- let info = lines.split("onDate ");
- let name = info[0].trim();
- let date = info[1];
- let movie = list.find((movieObj) => movieObj.name === name);
- if (movie) {
- movie.date = date;
- }
- }
- }
- for(const movie of list){
- if(movie.name && movie.director && movie.date){
- console.log(JSON.stringify(movie));
- }
- }
- }
- //2
- function movieDirectors(array) {
- let movies = [];
- class Movie {
- constructor(name, director, date) {
- this.name = name;
- this.director = director;
- this.date = date;
- }
- }
- let contains = function (movieName) {
- // let movie = null;
- let movie = movies.find(m => m.name === movieName);
- // movies.forEach(m => m.name === movieName ? movie = m : movie = null);
- return movie;
- }
- for (let i = 0; i < array.length; i++) {
- let command = array[i].split(' ');
- if (command.includes('addMovie')) {
- let name = command.slice(1, command.length).join(' ');
- movies.push(new Movie(name, null, null));
- } else if (command.includes('directedBy')) {
- let name1 = command.slice(0, command.indexOf('directedBy')).join(' ');
- // if (contains(name1) !== null) {
- if (contains(name1) !== undefined) {
- let movie = contains(name1);
- movie.director = command.slice(command.indexOf('directedBy') + 1, command.length).join(' ');
- }
- } else if (command.includes('onDate')) {
- let name2 = command.slice(0, command.indexOf('onDate')).join(' ');
- // if (contains(name2) !== null) {
- if (contains(name2) !== undefined) {
- let movie = contains(name2);
- movie.date = command.slice(command.indexOf('onDate') + 1, command.length).join(' ');
- }
- }
- }
- movies.forEach(m => m.director != null && m.name != null && m.date != null ? console.log(JSON.stringify(m)) : null);
- }
- // -----------------------------------------------------------------------------------------------------
- // 05. Inventory
- function inventory(array) {
- let heroes = [];
- class Hero {
- constructor(name,level,items){
- this.name = name,
- this.level = level,
- this.items = items
- }
- }
- for(const data of array) {
- let [name, level, items] = data.split(' / ');
- let currentHero = new Hero(name,Number(level),items);
- heroes.push(currentHero);
- }
- heroes.sort((a, b) =>
- a.level - b.level
- );
- for (const hero of heroes) {
- console.log(`Hero: ${hero.name}\nlevel => ${hero.level}\nitems => ${hero.items}`)
- }
- }
- // -----------------------------------------------------------------------------------------------------
- // 06. Word Tracker
- function solve(text) {
- let objects = {};
- let words = text.shift().split(' ')
- for (let word of words) {
- let occurances = 0;
- for (let i = 0; i < text.length; i++) {
- if (text[i] === word)
- occurances++;
- }
- objects[word] = occurances;
- }
- let result = Object.entries(objects).sort((a, b) => b[1] - a[1]);
- result.forEach(element => {
- console.log(`${element[0]} - ${element[1]}`)
- });
- }
- // -----------------------------------------------------------------------------------------------------
- // 07. Odd Occurrences
- function solve(text) {
- let result = [];
- let words = text.toLowerCase().split(' ');
- for (let word of words) {
- let occurances = 0;
- for (let i = 0; i < words.length; i++) {
- if (words[i] === word)
- occurances++;
- }
- if (occurances % 2 !== 0 && !result.includes(word)) {
- result.push(word)
- }
- }
- console.log(...result)
- }
- // -----------------------------------------------------------------------------------------------------
- // 08. Piccolo
- function solve(input) {
- let parking = [];
- for (let i = 0; i < input.length; i++) {
- let details = input[i].split(', ')
- let direction = details[0];
- let carNumber = details[1];
- if (direction === 'IN' && !parking.includes(carNumber)) {
- parking.push(carNumber);
- } else if (direction === 'OUT' && parking.includes(carNumber)) {
- let index = parking.indexOf(carNumber);
- parking.splice(index, 1)
- }
- }
- if (parking.length > 0) {
- console.log(parking.sort().join('\n'))
- } else {
- console.log('Parking Lot is Empty')
- }
- }
- // -----------------------------------------------------------------------------------------------------
- // 09. Make a Dictionary
- function solve(input) {
- let myDict = {};
- input.forEach(element => {
- let [rawTerm, rawDefinition] = (element.split('":"'))
- let term = rawTerm.slice(2, rawTerm.length)
- let definition = (rawDefinition.replace('"}', ''))
- myDict[term] = definition
- });
- for (const [key, value] of Object.entries(myDict).sort()) {
- console.log(`Term: ${key} => Definition: ${value}`)
- }
- }
- // -----------------------------------------------------------------------------------------------------
- // 10. Class Vehicle
- class Vehicle {
- constructor(type, model, parts, fuel) {
- this.type = type;
- this.model = model;
- this.parts = parts;
- this.parts.quality = parts.engine * parts.power;
- this.fuel = fuel;
- }
- drive(fuelLoss) {
- this.fuel -= fuelLoss;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement