JenMorgan / js-learning

0 stars 0 forks source link

Calculate the Sum of Users Groseries #37

Open JenMorgan opened 4 years ago

JenMorgan commented 4 years ago
const users = {
    user1: {
        tomatoes: 5,
        beer: 2,
        chips: 4,
        currency: "dollar"
    },
    user2: {
        tomatoes: 7,
        beer: 3,
        chips: 0,
        currency: "euro"
    },
    user3: {
        tomatoes: 15,
        beer: 5,
        chips: 1,
        currency: "byn"
    }
};

let finalPrice = {};

function GetTheTotalPrice (obj) {
    for (let key in obj) {
        let price = 0;
        let obj2 = obj[key];
        for (let i in obj2) {
            if (typeof(obj2[i]) === "number") {
                price += obj2[i];
            }
        }
        let currency;
        for (let i in obj2) {
            if (i === "currency") currency = obj2[i]
            else continue;
        }

        switch (currency) {
            case "dollar":
                price = `${price} (USD)`;
                break;
            case "euro":
                price = `${price} (EUR)`;
                break
            case "byn":
                price = `${price} (BYN)`;
                break;
        }
        finalPrice[key] = price;
    }
    return finalPrice;
}

GetTheTotalPrice(users);
console.log(finalPrice);