dimanche 1 février 2015

how to restrict javascript function to only accept one argument


I've created a simple javascript function that determines the nth fibonacci number. It works, but I am trying to figure out why it still works when it is passed two arguments.



function fib(n){
if (n <= 1){
return n;
}
else if(n > 1){
return fib(n-1) + fib(n-2);

}
}


So when I run fib(10) I get the correct output of 55. But if I were to do something like



fib(1,2)


I would expect to get Undefined, but the method is outputting 1.


My question is how can I keep a javascript method to only accept a certain number of arguments? In my case I only want fib to accept one argument.





Aucun commentaire:

Enregistrer un commentaire