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

Q: Auto-Engineering Company (JavaScript task)

+3 votes

You are tasked to create a register for a company that produces cars. You need to store how many cars have been produced from a specified model of a specified brand.

The input comes as array of strings. Each element holds information in the following format:

“{carBrand} | {carModel} | {producedCars}”

The car brands and models are strings, the produced cars are numbers. If the car brand you’ve received already exists, just add the new car model to it with the produced cars as its value. If even the car model exists, just add the given value to the current one.

As output you need to print – for every car brand, the car models, and number of cars produced from that model. The output format is:

“{carBrand}

  ###{carModel} -> {producedCars}

  ###{carModel2} -> {producedCars}

  ...”

The order of printing is the order in which the brands and models first appear in the input. The first brand in the input should be the first printed and so on. For each brand, the first model received from that brand, should be the first printed and so on.

Examples

Input

Output

Audi | Q7 | 1000

Audi | Q6 | 100

BMW | X5 | 1000

BMW | X6 | 100

Citroen | C4 | 123

Volga | GAZ-24 | 1000000

Lada | Niva | 1000000

Lada | Jigula | 1000000

Citroen | C4 | 22

Citroen | C5 | 10

Audi

###Q7 -> 1000

###Q6 -> 100

BMW

###X5 -> 1000

###X6 -> 100

Citroen

###C4 -> 145

###C5 -> 10

Volga

###GAZ-24 -> 1000000

Lada

###Niva -> 1000000

###Jigula -> 1000000

 

asked in JavaScript category by user mm
edited by user golearnweb

1 Answer

+2 votes

Here you go:

function autoCompany(array) {
    let map=new Map();
    for (let arr of array) {
        [carBrand,carModel,number]=arr.split(' \| ');
        number=Number(number);

        if(!map.has(carBrand)){
            map.set(carBrand,new Map());
        }
        if(!map.get(carBrand).has(carModel)){
            map.get(carBrand).set(carModel,0);
        }
        let oldSum=map.get(carBrand).get(carModel);
        map.get(carBrand).set(carModel,oldSum+number);

    }


    for (let [carBrand,carModels] of map) {
        console.log(carBrand);
        for (let [carModel,number] of carModels) {
            console.log(`###${carModel} -> ${number}`);
        }
    }
}

autoCompany([
    'Audi | Q7 | 1000',
    'Audi | Q6 | 100',
    'BMW | X5 | 1000',
    'BMW | X6 | 100',
    'Citroen | C4 | 123',
    'Volga | GAZ-24 | 1000000',
    'Lada | Niva | 1000000',
    'Lada | Jigula | 1000000',
    'Citroen | C4 | 22',
    'Citroen | C5 | 10'
]);

 

answered by user golearnweb
...