简体   繁体   中英

passing parameters to javascript function after function invocation?

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.");

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;
  }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM