-2

Javascript: How to pass functions as parameters?

The code is supposed to successfully run the function values that are passed into the loop function.

Any help would be greatly appreciated.

  function loop(value, test, update, body){
    for (let i = 0; i < 10; i++){
      if (test(i, value)) break;
      update(value);
      console.log(body(i));
    }
  }

  loop(5, (i, value) => { return i > value }, (value) => {return value + 1}, (i) => {return i*2;});
65535
  • 523
  • 1
  • 10
  • 32
  • Right, I have edited update. The question is correct now, please remove the negative and refresh it. – 65535 Jul 20 '20 at 00:19
  • 2
    Does this answer your question? [What is the scope of variables in JavaScript?](https://stackoverflow.com/questions/500431/what-is-the-scope-of-variables-in-javascript) – Phil Jul 20 '20 at 00:21
  • 1
    Use “test(i, value)” _and_ update the function to accept such: “loop(5, (i, v) => i > v, ..)” — note that “value” (parameter of the “loop” function) and “v” (parameter of the supplied anonymous function) are in _distinct scopes_, hence the name change for illustration clarity.. – user2864740 Jul 20 '20 at 00:26
  • Does this answer your question? [What is the scope of variables in JavaScript?](https://stackoverflow.com/questions/500431/what-is-the-scope-of-variables-in-javascript) – evolutionxbox Jul 20 '20 at 00:40
  • "Runnable snippets" dont appear to run in my browser. – 65535 Jul 20 '20 at 00:42
  • 1
    Give all the variables unique names. Calling things “i” and “value” are confusing things here. You don’t pass the correct number of arguments to “test”, “update”, nor “body”. – evolutionxbox Jul 20 '20 at 00:42
  • 1
    @65535 they run in mine, and perhaps in most other people’s. – evolutionxbox Jul 20 '20 at 00:43
  • Question is totally correct now, right? – 65535 Jul 20 '20 at 00:55
  • Still browser error: `TypeError: test is not a function`. – 65535 Jul 20 '20 at 00:57
  • 1
    @65535 how would I know the error? You removed the runnable snippet. – evolutionxbox Jul 20 '20 at 01:05
  • Check the edited question code, it is correct code and should work right? – 65535 Jul 20 '20 at 01:08
  • 1
    What browser are you using? Your current code throws no errors for me – Phil Jul 20 '20 at 01:08
  • I only run free and open source not Google, Microsoft, and Firefox spyware systems – 65535 Jul 20 '20 at 01:09
  • Are you serious right now!? You've stated the code snippets don't run in _your browser_ and that you get an error that nobody else is getting so I'm asking you, for the very last time, what browser are you using? You may be trying to use a feature that is [not enabled or implemented in _your browser_](https://caniuse.com/#feat=arrow-functions) so if you actually answer the question with a real answer like _"I'm using Brave"_, then we might be able to help you – Phil Jul 20 '20 at 01:13
  • It appears that code in the question is now 100% correct and error free, and the cause of the browser console error is a result of the browser, right? – 65535 Jul 20 '20 at 01:15

1 Answers1

0

Solution:

This solution does not differ from the code in the question because the original question was edited for certain purposes. However, the initial cause of the question was that the necessary parameters to the functions values were not present inside the loop function. For instance, i and value, had been missing here: test(i, value).

 function loop(value, test, update, body){
    for (let i = 0; i < 10; i++){
      if (test(i, value)) break;
      update(value);
      console.log(body(i));
    }
  }

  loop(5, (i, value) => { return i > value }, (value) => {return value + 1}, (i) => {return i*2;});
65535
  • 523
  • 1
  • 10
  • 32