settingsAccountsettings
By using our mini forum, you acknowledge that you have read and understand our Cookie Policy and Privacy Policy
Menusettings

Q: City Markets - JavaScript task

+4 votes

You have been tasked to follow the sales of products in the different towns. For every town you need to keep track of all the products sold, and for every product, the amount of total income.

The input comes as array of strings. Each element will represent data about a product and its sales. The format of input is:

{town} -> {product} -> {amountOfSales} : {priceForOneUnit}

The town and product are both strings. The amount of sales and price for one unit will be numbers. Store all towns, for every town, store its products, and for every product, its amount of total income. The total income is calculated with the following formula - amount of sales * price for one unit. If you receive as input a town you already have, you should just add the new product to it.

Example:

Input:
Sofia -> Laptops HP -> 200 : 2000
Sofia -> Raspberry -> 200000 : 1500
Sofia -> Audi Q7 -> 200 : 100000
Montana -> Portokals -> 200000 : 1
Montana -> Qgodas -> 20000 : 0.2
Montana -> Chereshas -> 1000 : 0.3

Output:
Town - Sofia
$$$Laptops HP : 400000
$$$Raspberry : 300000000
$$$Audi Q7 : 20000000
Town - Montana
$$$Portokals : 200000
$$$Qgodas : 4000
$$$Chereshas : 300


As output you must print every town, its products and their total income in the following format:
“Town – {townName}
 $$${product1Name} : {productTotalIncome}
 $$${product2Name} : {productTotalIncome}
 ...”

 The order of output for each of those entries is – by order of entrance.

asked in JavaScript category by user Jolie Ann

1 Answer

+2 votes

Here is my solution:

//NE E RESHENA!
function cityMarkets(sales) {

    let townsWithProducts = new Map();
    for (let sale of sales) {

        let [town,product,quantityPrice]=sale.split(/\s*->\s*/);
        let [quantity,price]=quantityPrice.split(/\s*:\s*/);
        if (!townsWithProducts.has(town)) {
            townsWithProducts.set(town, new Map());
        }
        let income = quantity * price;
        let oldIncome = townsWithProducts.get(town).get(product);
        if (oldIncome) {
            income += oldIncome;
        }
        townsWithProducts.get(town).set(product, income);
    }

    let sortedTowns = Array.from(townsWithProducts.entries()).sort(mySort);

    //or:
    //[...townsWithProducts.entries()].sort(mySort);

    for (let [town,productsMap] of sortedTowns) {
        console.log("Town - " + town);
        for (let [name,price] of productsMap) {
            console.log(`$$$${name} : ${price}`);
        }
    }


    function mySort(a, b) {
        let aLength = a[0].length;
        let bLength = b[0].length;
        let firstCriteria = aLength - bLength;
        if (firstCriteria !== 0) {
            return firstCriteria;
        } else {
            return a[0].localeCompare(b[0]);//compare alphabetically vazhodyasht red
            return b[0].localeCompare(a[0]);//compare alphabetically nizhodyasht red
        }
    }
}

cityMarkets([
    "Sofia -> Laptops HP -> 200 : 2000",
    "Sofia -> Raspberry -> 200000 : 1500",
    "Sofia -> Audi Q7 -> 200 : 100000",
    "Montana -> Portokals -> 200000 : 1",
    "Montana -> Qgodas -> 20000 : 0.2",
    "Montana -> Chereshas -> 1000 : 0.3"
]);

 

answered by user matthew44
...