# Smallest job first algorithm implementation

I have implemented smallest job first algorithm using javascript. It was part of a coding challenge. I would like to ask for evaluation of my script. I would like to get suggestions to improve my code performance. Which parts of the code I can improve. Is there an alternative to for loops?

Function takes jobs array with transaction time and index. Returns the clock cycles of the process/job at specific index. Function Call with sample input, sjf([10, 3, 15, 8, 7], 3).

Here is the code.

function sjf(jobs, index) {

// declare waiting time array
var waitingTime = [];
// set waiting time for first task to zero
waitingTime = 0;

// declare total time array
var totalTimeArray = [];

var processArray = [];
// add job value and index as an object to process array
for (i=0; i < jobs.length; i++) {
processArray.push({
id: i,
value: jobs[i]
})
}

// sort the array in ascending order
function sortArray(a, b) {
return a.value - b.value;
}

// calculate waiting time for each process except first process
// waiting time for first process is zero so we start to calculate from index 1
function calculateWaitingTime(sortedArray, waitingTime) {
for (i=1; i < sortedArray.length; i++) {
waitingTime.push(sortedArray[i-1].value + waitingTime[i-1]);
}
}

// total time taken to complete each task
for (i=0; i < sortedArray.length; i++) {
totalTimeArray.push({
id: sortedArray[i].id,
time: sortedArray[i].value + waitingTime[i]
});
}
}

// find clock cycles
function findClockCycles(totalTimeArray, index) {
for (i = 0; i < totalTimeArray.length; i++) {
if (totalTimeArray[i].id === index) {
}
}
}

// First of all sort the process array in ascending order of their value
var sortedArray = processArray.sort(sortArray);

// calculate waiting time for the rest of the processes
calculateWaitingTime(sortedArray, waitingTime);

// calculate total time for each task

// return clock cycles for the task
return findClockCycles(totalTimeArray, index);
}


I think this can be done with far less code. Like this:

function calculateTotalClockCycles(jobs, index) {
jobs.sort((a, b) => a - b);
return jobs.slice(0, index).reduce((a, b) => a + b, 0);
}

alert(calculateTotalClockCycles([10, 3, 15, 8, 7], 3));


In the first line of code inside the function, the jobs are sorted, small to large. The second line is more complicated. The reduce() method is used to sum all the values of the array it applies to. That array is the wanted part (slice) of the whole sorted jobs array.

So here reduce() is used as an alternative for a 'for' loop.

• This is merely 3 lines of code. I had no idea, that it could be solved like this. I am wondering why nothing came into my mind for such a solution. @KIKO do this school of thought belongs to functional programming? And what steps could I take to polish my skills and thought process to create solutions like you have provided. – Chasmatu Sep 28 '18 at 9:04
• In my opinion it all comes down to paying attention to what others do, reading 'books', and a lot of practice. You have to be able to think logically and to reduce a problem to its essence. I've been doing this for 36 years, you can't beat that. However, I actually had to look up the Javascript methods I used, so I guess that part of it is not being afraid of reading documentation. I used this: developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/… It's all there. That said: I don't exclude the possibility that there are better/nicer solutions possible. – KIKO Software Sep 28 '18 at 9:46
• Any reason you used an arrow function for reduce, but the longer function syntax for sort? – Gerrit0 Sep 28 '18 at 16:24
• @Gerrit0 No, didn't spent any thought on that. Those were the examples I found in the documentation, and used. You could indeed write (a, b) => a - b in the sort method. That looks nice. I'll replace the function(a, b) { return a - b; } with it. Thanks for the tip. – KIKO Software Sep 28 '18 at 18:05
• @KIKOSoftware I just realized that this answer might be wrong. The current solution sorts the array and after sorting the index of lets say number 8 changes to 2 where as in original array it is 3. As far as I understood, I needed to calculate the time of the task against original index and not the index of the sorted array. – Chasmatu Oct 2 '18 at 10:23