0

I made a "fake DB" with template literals and I made it into an employee list with different attributes like name, salary, address, phone number, and most importantly revenue for that month. I would like to create a sort of scanner which would loop through all employees and print out the 3 best earning employees and in a different card I'd like to print out 3 "worst-performing" employees.

How would I go on to pull off the things mentioned above? I've only managed to print only one, I'll add the code above. Hope you guys can help me out on this one.

In case somebody wants to know, i'm using BULMA, Sass and chart.js.

What I tried

// Print best selling employee
const bestSellingEmployees = () => {

    let biggestRevenue = 0;
    let htmlTxt = "";

    EmployeeModule.getAll().forEach(employee => {

        if(employee.revenue >= biggestRevenue) {
            htmlTxt = `
            
                <img src="images/employee-images/${employee.img}" width="50%" class="is-horizontal-center" alt="Bilde ${employee.name}">
                <h6 class="title is-6 has-text-centered mt-5">${employee.name}</h6>

                <div class="columns ml-4">

                    <div class="column">
                        <p>Profit</p> 
                        <p>400 000</p>
                    </div>

                    <div class="column">
                        <p>Income</p> 
                        <p>${employee.revenue}</p>
                    </div>

                </div>
            
            `;
        }
    })

    bestPerformingEmployee.innerHTML = htmlTxt;
} 

employee.html

<!--Card start-->
          <div id="bottom-3" class="card mt-5">

            <!--Card content-->
            <div class="card-content">

              <!--Card title-->
              <h5 class="title is-5">Populære kategorier</h5>

              <div class="content">

                Lorem ipsum leo risus, porta ac consectetur ac, vestibulum at eros. Donec id elit non mi porta gravida
                at eget metus. Cum sociis natoque penatibus et magnis dis parturient montes, nascetur ridiculus mus.
                Cras mattis consectetur purus sit amet fermentum.

              </div>
            </div>
          </div>
          <!--Card end-->

employeeModule.js

const EmployeeModule = ( function(){

    //employeeArray start
    const employeeArray = [

        {
            name: "Jan Nordmann",
            address: "Vollsveien 137b",
            phoneNumber: "46983312",
            revenue: 15392,
            salary: 188,
            position: "Deltid",
            img: "employee-avatar.png"

        },
        {
            name: "Cecillie Holme",
            address: "Sørumgata 3",
            phoneNumber: "92591093",
            revenue: 32574,
            salary: 192,
            position: "Fulltid",
            img: "employee-avatar.png"
        },
        {
            name: "Suresh Rajesh",
            address: "Holmlia Senter vei 3",
            phoneNumber: "93589012",
            revenue: 42021,
            salary: 200,
            position: "Fulltid",
            img: "employee-avatar.png"
        },
        {
            name: "Elias Khalil",
            address: "Aslakveien 1",
            phoneNumber: "43492316",
            revenue: 9026,
            salary: 171,
            position: "Vikar",
            img: "employee-avatar.png"
        },
        {
            name: "Andrea Ris",
            address: "Holtegata 8",
            phoneNumber: "9201942",
            revenue: 13102,
            salary: 171,
            position: "Vikar",
            img: "employee-avatar.png"
        },
        {
            name: "Noah Amundsen",
            address: "Askeladveien 3",
            phoneNumber: "43363366",
            revenue: 21023,
            salary: 180,
            position: "Deltid",
            img: "employee-avatar.png"
        },
        {
            name: "Gregorz Braun",
            address: "Trosterudveien 38",
            phoneNumber: "43363366",
            revenue: 20301,
            salary: 171,
            position: "Vikar",
            img: "employee-avatar.png"
        },
        {
            name: "Märta Vittsjö",
            address: "Kirkerudveien 20",
            phoneNumber: "43363366",
            revenue: 14212,
            salary: 171,
            position: "Vikar",
            img: "employee-avatar.png"
        },
        {
            name: "Max Felix",
            address: "Storgaten 59",
            phoneNumber: "43363366",
            revenue: 39890,
            salary: 179,
            position: "Fulltid",
            img: "employee-avatar.png"
        },
        {
            name: "Nicholas Kjellberg",
            address: "Bergensgate 95",
            phoneNumber: "43493131",
            revenue: 29401,
            salary: 171,
            position: "Vikar",
            img: "employee-avatar.png"
        },
        {
            name: "Ben McDonald",
            address: "Midtveien 169",
            phoneNumber: "49482911",
            revenue: 18594,
            salary: 184,
            position: "Deltid",
            img: "employee-avatar.png"
        },
        {
            name: "Jakob Sæther",
            address: "Dr. Dedichens vei 20",
            phoneNumber: "92583901",
            revenue: 39394,
            salary: 199,
            position: "Fulltid",
            img: "employee-avatar.png"
        },
        {
            name: "Nora Gulbrandsen",
            address: "Toftes gate 12",
            phoneNumber: "40019313",
            revenue: 28491,
            salary: 171,
            position: "Vikar",
            img: "employee-avatar.png"
        },
        {
            name: "Hoyo Ahmed",
            address: "Stovner senter gata 30",
            phoneNumber: "93194212",
            revenue: 102941,
            salary: 171,
            position: "Vikar",
            img: "employee-avatar.png"
        },
        {
            name: "Bengt Husby",
            address: "Husbygata 12",
            phoneNumber: "49392911",
            revenue: 39890,
            salary: 189,
            position: "Fulltid",
            img: "employee-avatar.png"
        },
        {
            name: "Harald Pedersen",
            address: "Frydenbergveien 43D",
            phoneNumber: "43493131",
            revenue: 22812,
            salary: 171,
            position: "Vikar",
            img: "employee-avatar.png"
        },
        {
            name: "Amir Khan ",
            address: "Glitre veien 31",
            phoneNumber: "49482911",
            revenue: 18594,
            salary: 184,
            position: "Deltid",
            img: "employee-avatar.png"
        },
        {
            name: "Laila Sharoon",
            address: "Sorgenfrigata 12",
            phoneNumber: "92583901",
            revenue: 51362,
            salary: 200,
            position: "Fulltid",
            img: "employee-avatar.png"
        },
        {
            name: "Emilie Jakobssen",
            address: "Krafts gate 8",
            phoneNumber: "92481021",
            revenue: 28190,
            salary: 180,
            position: "Deltid",
            img: "employee-avatar.png"
        },
    
    ];
    
    //employeeArray end
    const getAll = () => employeeArray;

    return {getAll};

    
})

export default EmployeeModule;
  • 1
    What are you asking? How to get the 3 best & 3 worst employees from a list? Or how to render a list? – jarmod May 28 '21 at 12:10
  • The looping has nothing to do with template literals. You'd have the same problem - a single `htmlText` variable that you overwrite instead of appending - with normal string literals. – Bergi May 28 '21 at 12:21
  • @jarmod I would like to create two containers where I would print out different info. In the first one I would print out top 3 earning employees and then I would like to print out the info about bottom 3 earning employees on another card. – hackandslash May 28 '21 at 13:46
  • @Bergi How would I pull it off then? Ayn suggestions? Also it's employees, i used the code from another file that belongs to the project and forgot to change all of the text from restaurant to employee, my bad. – hackandslash May 28 '21 at 13:46
  • @hackandslash [`sort` them by earnings](https://stackoverflow.com/questions/979256/sorting-an-array-of-objects-by-property-values), [`slice`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/slice) the top/bottom 3, [`map`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/map) them to an array of strings, [`join`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/slice) these arrays to strings. Or do the last steps with a manual loop and string concatenation. – Bergi May 28 '21 at 15:04

0 Answers0