TaskCollection's get method is particularly tricky, as it has so many different modes of input. Here's a suggestion on how to structure things:
First of all, you have to understand how "get" is meant to handle the first type of input, that of a function. Think of how a call would look like:
var t = collection.get(f);
Where f is a function. What should happen in this call is that we will go through each task in turn, and call f on it, i.e. f(task). If the result is true we have found the task and we can return it from get. If it is not then we move to the next task in the array and so on.
You should start by implementing "get" as if this was the only possible form for the parameter. Make sure you get that going.
After you have completed this, you need to expand "get" to all these other types of parameters. The trick is this: Turn all those other types into a function, then you don't need to change "get" at all. So you could be doing something like this:
function makeFunctionFromArg(arg) {
... if arg is a function return it
... if arg is a number return the function: "function(task) { return task.id === arg; }"
... Turn each other case into a function
}
function get(arg) {
arg = makeFunctionFromArg(arg);
... your code for handling function-type arg
}
Hope this can give you a starting point.
TaskCollection's get method is particularly tricky, as it has so many different modes of input. Here's a suggestion on how to structure things:
First of all, you have to understand how "get" is meant to handle the first type of input, that of a function. Think of how a call would look like:
Where
fis a function. What should happen in this call is that we will go through each task in turn, and call f on it, i.e.f(task). If the result istruewe have found the task and we can return it from get. If it is not then we move to the next task in the array and so on.You should start by implementing "get" as if this was the only possible form for the parameter. Make sure you get that going.
After you have completed this, you need to expand "get" to all these other types of parameters. The trick is this: Turn all those other types into a function, then you don't need to change "get" at all. So you could be doing something like this:
Hope this can give you a starting point.