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

Q: Populations in Towns - JavaScript task

+3 votes

You have been tasked to create a register for different towns and their population.

The input comes as array of strings. Each element will contain data for a town and its population in the following format:
“{townName} <-> {townPopulation}”

If you receive the same town twice, you should add the given population to the current one.

Examples:

Input:
Sofia <-> 1200000
Montana <-> 20000
New York <-> 10000000
Washington <-> 2345000
Las Vegas <-> 1000000

Output:
Sofia : 1200000
Montana : 20000
New York : 10000000
Washington : 2345000
Las Vegas : 1000000


Input:
Istanbul <-> 100000
Honk Kong <-> 2100004
Jerusalem <-> 2352344
Mexico City <-> 23401925
Istanbul <-> 1000

Output:
Istanbul : 101000
Honk Kong : 2100004
Jerusalem : 2352344
Mexico City : 23401925


As output, you must print all the towns, and their population.

asked in JavaScript category by user john7

1 Answer

+2 votes

Here's my javascript solution to this task:

function populationInTown(dataRows) {
    let total = new Map();
    for (let dataRow of dataRows) {
        let [town, population]=dataRow.split(/\s*<->\s*/);
        population = Number(population);
        if (total.has(town)) {
            total.set(town, total.get(town) + population);
        } else {
            total.set(town, population);
        }
    }

    for (let [town,sum] of total) {
        console.log(town + " : " + sum);
    }
}

//populationInTown([
//        "Sofia <-> 1200000",
//        "Montana <-> 20000",
//        "New York <-> 10000000",
//        "Washington <-> 2345000",
//        "Las Vegas <-> 1000000"
//    ]
//);

populationInTown([
        "Istanbul <-> 100000",
        "Honk Kong <-> 2100004",
        "Jerusalem <-> 2352344",
        "Mexico City <-> 23401925",
        "Istanbul <-> 1000"
    ]
);

 

answered by user Jolie Ann
...