Javascript: How to make my script better / readable?

Dear Sir / Madam,

Hope you all are doing well, and stay safe & healthy

I just got a personal project that would like to brush up my skill. Luckily, the script works, but I believe it is not that readable and efficient.

May I know if anyone could lend me a helping hand to improve my script, please ?

Much appreciated.

Samson

// script is as below:
// Project: Staff Checking Test
// Ultimate Objectives of this project
// 1. Try to think of the whole operation and use of functions
// 2. Try to use the [ if command ] to run

// Tasks:
// 1. Checking which staff is eligible to use the rCMS system
// 2. Checking which staff is elighble to use the rCMS system and also know how to upload data into it
// 3. Checking uploading skill for the eligible staff
// 4. Finding the highest score / percentage among the eligible staff

// Objective: Adding / editing the Assiociative Arrays
var staff1 = { name: ‘Samson’, sex: ‘male’, identity: ‘S2820’, rCMS: ‘eligible’, dataUpload: 20 };
// testing: console.log (staff1);
// testing: for (var key in staff1) {
// testing: console.log(key + ’ is ’ + staff1[key]);
// testing: };
var staff2 = { name: ‘Helenna’, sex: ‘female’, identity: ‘S2255’, rCMS: ‘eligible’, dataUpload: 10 };
var staff3 = { name: ‘Lawrence’, sex: ‘female’, identity: ‘S8856’, rCMS: ‘eligible’, dataUpload: 4 };
var staff4 = { name: ‘Tony’, sex: ‘male’, identity: ‘S1236’, rCMS: ‘eligible’, dataUpload: 0 };
var staff5 = { name: ‘Bryan’, sex: ‘male’, identity: ‘S4635’, rCMS: ‘non-eligible’, dataUpload: 0 };
var staff6 = { name: ‘Bernard’, sex: ‘male’, identity: ‘S7766’, rCMS: ‘eligible’, dataUpload: 3 };
var staff7 = { name: ‘Hagudeen’, sex: ‘male’, identity: ‘S7766’, rCMS: ‘eligible’, dataUpload: 100 };

// Objective: For calculating totalNoOfDataUpload when new member joined in, need to add staff_.dataUpload at the back.
var totalNoOfDataUpload = staff1.dataUpload + staff2.dataUpload + staff3.dataUpload + staff4.dataUpload + staff5.dataUpload + staff6.dataUpload + staff7.dataUpload;
console.log ('The total numbers of dataUpload gathered by all staff: ’ + totalNoOfDataUpload);

// Objective: Showing the staff information, who get the highest score / percentage in uploading data to rCMS
var highestScore = Math.max(staff1.dataUpload, staff2.dataUpload, staff3.dataUpload, staff4.dataUpload, staff5.dataUpload, staff6.dataUpload, staff7.dataUpload);
console.log('The highest score of dataUpload among staff: ’ + highestScore);

// Objective: Finding the staff who is eligible and able to upload, eligible but not able to upload, and non-eligble
function staffChecking (staffNo) {
if ( staffNo.rCMS === ‘eligible’ && staffNo.dataUpload > 0 ) {
console.log(staffNo.name + " is eligible in obtaining " + (Math.trunc(staffNo.dataUpload / totalNoOfDataUpload *100)) + "% of skill to upload rCMS. ")
} else if (staffNo.rCMS === ‘eligible’ && staffNo.dataUpload === 0 ) {
console.log (staffNo.name + " is eligible but don’t know how to upload rCMS. ");
} else if (staffNo.rCMS !== ‘eligible’) {
console.log(staffNo.name + " is not eligible to upload rCMS. ");
}
};

// Objective: find the staff with highest score of uploading rCMS
function getHighestScoreOfStaff (staffScore) {
if (staffScore.dataUpload === highestScore) {
window.alert (‘The winner of the year 2020: ’ + staffScore.name + ’ ,whose staff identity is ’ + staffScore.identity+ ’ ,got the highest scores in uploading rCMS with more than ’ + highestScore + ’ times!!!’);
}
};

// Objective: Use this to let the argument(e.g staff1, staff2 etc) be copied to parameter staffNo under function staffChecking ()
staffChecking(staff1);
staffChecking(staff2);
staffChecking(staff3);
staffChecking(staff4);
staffChecking(staff5);
staffChecking(staff6);
staffChecking(staff7);

// Objective: Use this to let the argument(e.g staff1, staff2 etc) be copied to parameter staffNo under function getHighestScoreOfStaff ()
getHighestScoreOfStaff (staff1);
getHighestScoreOfStaff (staff2);
getHighestScoreOfStaff (staff3);
getHighestScoreOfStaff (staff4);
getHighestScoreOfStaff (staff5);
getHighestScoreOfStaff (staff6);
getHighestScoreOfStaff (staff7);

// Questions
// Q.1. How to modify and make the scripts look better and readable ?
// Q.2. Whenever I enter a new Associative Array e.g staff8, I need to do extra four steps further, including Step 1 - adding new staff.dataUpload at the back in variable totoalNoOfDataUpload. Step 2 - call function staffChecking () by entering new argument e.g staffChecking(staff8),
// … Step 3 - Adding new staff.dataUpload at the back in variable highestScore. Step 4 - call function getHighestScoreOfStaff () by entering new argument e.g getHighestScoreOfStaff(staff8). Would it be able to shorten the steps to make it run efficiently ?
// Q.3 For the [ if command ] I use, is there any better command would be better than this to run the program ?

// Much appreciated if anyone could share with me the professional feedbacks, thank you so much !!!

1 Like

Hello, Samson!

Did you get the answer you want already?
Making a script better is very important. If not yet, I might be able to help you. I’m waiting for your comment here.

Good luck!