This code will return two references to undefined
.
The first reference being the call of the immediate function declared in line #1.
The second reference being the call of the nested function: media()
.
The reason for the return of the reference to undefined
of function media()
is along that line:
for(i = 0; i < qtd; i++) {
The test condition of the loop for is: while i
is less than qtd
.
Being qtd
the number of arguments passed to the function media
and i igual a 0
, then when the function does not receive any argument we have: qtd igual a 0
.
Zero is not less than zero, but equal.
This makes the loop for is not executed, causing the next instruction to be executed after the loop for, this statement being a sum with a non-existent value - because there is no passage of arguments, being arguments[i]
non-existent, this non-existent value is evaluated in undefined
.
We know that any operation with undefined
is resulting in undefined
. Soon the return of the function is undefined
.
To treat this return, before the function returns statement, we can make an evaluation of the expression arguments[i]
, for example:
if(arguments[i] == undefined) console.log("Houve um erro")
Doing so treatment, but of course there is a vastness of other treatment possibilities.
You’re giving a
return
within a loopfor
, does not make sense. What is your intention with the code above?– Cmte Cardeal