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] = 0;

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

var processArray = [];
// add job value and index as an object to process array
for (i=0; i &lt; 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 &lt; sortedArray.length; i++) {
        waitingTime.push(sortedArray[i-1].value + waitingTime[i-1]);
    }
}

// total time taken to complete each task
function calculateTimeForEachTask(sortedArray, totalTimeArray) {
    for (i=0; i &lt; 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 &lt; totalTimeArray.length; i++) {
        if (totalTimeArray[i].id === index) {
            return totalTimeArray[i].time;
        }
    }
}

// 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
calculateTimeForEachTask(sortedArray, totalTimeArray);

// 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) =&gt; a - b);
  return jobs.slice(0, index).reduce((a, b) =&gt; 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.