0

I am making a function that returns an arbitrary term of a linear sequence. The test cases look like this:

Test.assertEquals(getFunction([0, 1, 2, 3, 4])(5), 5, "Nope! Try again.");
Test.assertEquals(getFunction([0, 3, 6, 9, 12])(10), 30, "Nope! Try again.");
Test.assertEquals(getFunction([1, 4, 7, 10, 13])(20), 61, "Nope! Try again.");

I don't understand the function invocation. I have written this code to determine the function of the linear sequence and compute an arbitrary term, but I don't know how to pass my function the term to compute:

function getFunction(sequence) {
  var diff = sequence[1] - sequence[0];
  var init = sequence[0];

  return diff*arguments[1]+init;
}

arguments[1] doesn't access the term passed in after the parameters. How can I access the term (5) in the first example?

Test.assertEquals(getFunction([0, 1, 2, 3, 4])(5), 5, "Nope! Try again.");
3
  • Your function must return a function. Commented Dec 19, 2014 at 21:51
  • you are only passing in your array to getFunction, there are no other arguments to get, hence why arguments[1] doesnt contain anything Commented Dec 19, 2014 at 21:53
  • As I read it, getFunction([...]) returns a function which takes 5 in argument (for the first test case), you won't reach the argument 5 from getFunction Commented Dec 19, 2014 at 21:55

1 Answer 1

2

You need to return a function from getFunction() in order to chain your function calls like your tests are expecting

Something like this:

function getFunction(sequence) {
  var diff = sequence[1] - sequence[0];
  var init = sequence[0];
  return function(num) {
     return diff*num+init;
  }
}
Sign up to request clarification or add additional context in comments.

1 Comment

ahhh, so if I had an invocation like getSequence(stuff)(more stuff)(even more stuff); I would just have to keep nesting return statements that return functions?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.