Sorting an Array by Input Value in JavaScript
This is a continuation of my new series of posts on algorithms. You can check out my previous one about finding the first non-repeating character in a string here.
While preparing for a technical interview this past week, I wanted to focus on sorting arrays. I came across this prompt and hated it at the start, but I came to appreciate how much it made me think. On top of that, I’m pretty happy with how I solved it!
The Prompt
You have some tasks in your account. For each ith
of them you know its deadlinesi
, which is the last day by which it should be completed. As you can see in your calendar, today's date is day
. The program labels each task in accordance with its due date:
- If the task is due today or it’s already overdue, it is labeled as Today;
- If the task is due within a week but not today — that is, its deadline is somewhere between
day + 1
andday + 7
both inclusive - it is labeled as Upcoming; - All other tasks are labeled as Later;
Given an array of deadlines
and today's date day
, your goal is to find the number of tasks with each label type and return it as an array with the format [Today, Upcoming, Later]
, where Today, Upcoming and Later are the number of tasks that correspond to that label.
Example
- For
deadlines = [1, 2, 3, 4, 5]
andday = 2
, the output should betasksTypes(deadlines, day) = [2, 3, 0]
. - Today is day
2
, so tasks with deadlines at1
and2
are labeled as Today. The other three tasks should be completed within a week, so they should be labeled as Upcoming. There are no tasks labeled as Later.
- For
deadlines = [1, 2, 4, 2, 10, 3, 1, 4, 5, 4, 9, 8]
andday = 1
, the output should betasksTypes(deadlines, day) = [2, 8, 2]
. - Today is day
1
, which means that the two tasks with a deadline of1
are labeled as Today. Tasks with deadlines10
and9
are labeled as Later, since their deadlines are more than7
days ahead. The other eight tasks are labeled as Upcoming.
My Solution
What a prompt, right? Luckily it’s much less complicated than the prompt makes it seem (in my opinion).
function tasksTypes(deadlines, day) {
let today = 0;
let upcoming = 0;
let later = 0;
const deadline = []; for (let i = 0; i < deadlines.length; i++) {
let dline = deadlines[i]; if (dline <= day) {
today += 1
} else if (dline >= day+1 && dline <= day+7) {
upcoming += 1
} else {
later += 1
}
} deadline.push(today);
deadline.push(upcoming);
deadline.push(later);
return deadline;
}
- We know that the tasks are going to be sorted into 3 categories:
today
,upcoming
, andlater
. As a result, the first thing I did was set variables for each of these initialized to 0, because they begin with 0 tasks assigned to them. - I also set the variable
deadline
to an empty array. This is where we’re going to push the values oftoday
,upcoming
, andlater
after we’ve sorted the input array. - To iterate through the data, I chose to use a
for
loop because I know how many iterations we need to make (the length of the input arraydeadlines
). - To isolate the individual deadline, I set the variable
dline
equal to the current index place. - Next, I used a series of
if
statements to sort thedeadlines
array. The first statement dictates thattoday
should be added to if the current index value in the deadlines array is less than or equal to the value ofday
. The next statement dictates thatupcoming
should be added to if the current index value in the deadlines array is greater than or equal the value ofday + 1
and less than or equal to the value ofday + 7
. The last statement covers the remaining parameters and adds 1 tolater
. - Following the
if
statement, I pushed the values oftoday
,upcoming
, andlater
(in that order due to the constraints of the prompt and, therefore, tests) into the deadline array. - Finally, I returned the deadline array that’s been populated with the values associated with
today
,upcoming
, andlater
.
This solution passed all 20 tests within the allotted 4 second run time. If you have another solution, please let me know! Happy coding!