-3

The book that I'm studying says about iterating over arrays with every that:

The function these methods use must follow one rule—it must accept three arguments like the following code:

function functionName(value, index, array) { 
// do something here
}

Does that mean that I must always use 3 arguments? If so then why does this code work?

var numbers = [ 1, 2, 2 ];
function isLessThan3(value) {
  var returnValue = false;
  if (value < 3) {
    returnValue = true;
}
return returnValue; }

document.write(numbers.every(isLessThan3));
John Kugelman
  • 349,597
  • 67
  • 533
  • 578
Karolina Ticha
  • 531
  • 4
  • 19
  • 3
    As a suggestion stop reading that book. "it must accept three arguments ..." is misleading. – Ram Aug 26 '16 at 21:08
  • Please be specific what you are asking. That function does not have the code inside. It is just for explaination. And you should learn what is array first. – Shahbaz Hashmi Aug 26 '16 at 21:10
  • 1
    Could you give more context about the quote from the book? What methods is it actually talking about when it says "these methods"? – Addison Aug 26 '16 at 21:11
  • 1
    Next time, read the MDN page about it, it explains it why https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/every – Braiam Aug 26 '16 at 21:41

3 Answers3

1

There is no limitation on how many arrguments you can put in a function with Javascript.

you have a very good explenation about this topic in the next answer by @Niet the Dark Absol https://stackoverflow.com/a/22747272/1283672

i believe that the book was reffering to something more specific within it's scope.

And just to be clear you can put no arrgs in a function either.

Community
  • 1
  • 1
fedesc
  • 2,554
  • 2
  • 23
  • 39
0

It's a bit ugly, the code, you have, but there is help. You might use the following without a temporary variable. Just return the result of the comparison.

function allLessThan3(value) {
    return value < 3;
}

var numbers = [1, 2, 2];
console.log(numbers.every(allLessThan3));
Nina Scholz
  • 376,160
  • 25
  • 347
  • 392
-1

No, you can use from 0 to 3 arguments

  • I can pass any number of arguments I like to a function... – John Dvorak Aug 26 '16 at 21:11
  • @JanDvorak yes, but in this case 4th and greater arguments gonna be `undefined` – Andrew Paramoshkin Aug 26 '16 at 21:14
  • What do you mean by that? – John Dvorak Aug 26 '16 at 21:15
  • @JanDvorak I mean if you execute this: `['a','b','c'].every(function(x,y,z,a,b,c) {console.log(a,b,c)})` you will see three `undefined`. – Andrew Paramoshkin Aug 26 '16 at 21:17
  • +1 - i believe she was asking a more basic question as of how many arguments can be inserted in a function. and gave a function with a single arg asking how it's working. you answered about usage of them arguments.. your answer is correct for a different question. – fedesc Aug 26 '16 at 21:19
  • Well, the question goes the other way... – John Dvorak Aug 26 '16 at 21:19
  • @FedeSc even then it's really poorly formulated. Also, answers should be rated relative to questions they answer... – John Dvorak Aug 26 '16 at 21:19
  • @JanDvorak I hear you! – fedesc Aug 26 '16 at 21:21
  • @FedeSc and again - any number of arguments can be passed to a function. If I pass more, they will be ignored. If I pass fewer, the rest will be undefined. If a function wants to test for the number of arguments it can, but it's not the default. – John Dvorak Aug 26 '16 at 21:22
  • 1
    @FedeSc Are you saying that the answer says "a function that names three slots for pieces of data can make use of three pieces of data"? Even if that was what the answer meant - and people were expected to interpret it that way - how is that ever useful to anyone? – John Dvorak Aug 26 '16 at 21:46