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

Q: Parse the Employee Data - JavaScript task

+3 votes

Write a JavaScript function that validates employee data, and stores it if it is valid. The employee data consists of 3 elements – employee name, employee salary and employee position.

The input comes as an array of strings. Each element represents input employee data. You should capture only the valid from them. The input will have the following format:

{employeeName} - {employeeSalary} - {employeePosition}

The Employee name will be a string, which can contain only English alphabet letters and must start with a capital. The Employee salary should be a VALID number. The employee position can contain English alphabet letters, digits, dashes, and can consist of several words. Any input that does NOT follow the specified above rules, is to be treated as invalid, and is to be ignored.

Examples:

Input:
Isacc - 1000 - CEO
Ivan - 500 - Employee
Peter - 500 - Employee

Output:
Name: Isacc
Position: CEO
Salary: 1000
Name: Ivan
Position: Employee
Salary: 500
Name: Peter
Position: Employee
Salary: 500


Input:
Jonathan - 2000 - Manager
Peter- 1000- Chuck
George - 1000 - Team Leader

Output:
Name: Jonathan
Position: Manager
Salary: 2000
Name: George
Position: Team Leader
Salary: 1000


The output should be printed on the console. For every valid employee data found, you should print each of its elements. Check the examples above.

asked in JavaScript category by user eiorgert

2 Answers

+1 vote

You should use regex and groups = that would be easier (see line 2 for the regex pattern):

function employeeDate(arrStr) {
    let pattern = /^([A-Z][a-zA-Z]*) - ([1-9][0-9]*) - ([a-zA-Z0-9 -]+)$/;

    for (let arrS of arrStr) {
        let match = pattern.exec(arrS);
        if (match) {
            console.log(`Name: ${match[1]}\n` + `Position: ${match[3]}\n` + `Salary: ${match[2]}`);
        }
    }
}


employeeDate([
    "Isaac - 1000 - CEO",
    "Ivan - 500 - Employee",
    "Peter - 500 - Employee"
]);

 

answered by user hues
function ed(input) {

    let regex = /^([A-Z][a-zA-Z]*) \- ([1-9][0-9]*) \- ([a-zA-Z0-9 -]+)$/;

    for (data of input) {
        let match = regex.exec(data);


        if (match) {
            console.log(`Name: ${match[1]}`);
            console.log(`Position: ${match[3]}`);
            console.log(`Salary: ${match[2]}`);
        }
    }
}
0 votes

Another solution (see line #6 to see how exec() is used):

function eD(arrStr) {

    let pattern = /^([A-Z][a-zA-Z]*)\s\-\s([1-9][0-9]*)\s\-\s([a-zA-Z0-9 -]+)$/g;

    for (let arrS of arrStr) {
        let matches = pattern.exec(arrS);//to get the information from the group(s) - returns an array with all the groups in the match

        while (matches) {
            console.log(`Name: ${matches[1]}`);
            console.log(`Position: ${matches[3]}`);
            console.log(`Salary: ${matches[2]}`);
            matches = pattern.exec(arrS);
        }
    }
}

eD([
    "Isaac - 1000 - CEO",
    "Ivan - 500 - Employee dsdsd",
    "Peter - 500 - Employee"
]);

 

answered by user golearnweb
...