I'm in an online course of JQuery where the teacher proposed a code like this:
var inicia = function(){
var valorTotalItens = $('.valor-total-item');
var valorBase = 0;
for (var i = 0; i < valorTotalItens.length; i++) {
var valorItemTotal = $(valorTotalItens[i]);
var valorItem = parseFloat(valorItemTotal.text());
valorBase = valorBase + valorItem;
}
$('#quantidade-itens').text(valorTotalItens.length);
$('#valor-itens').text(valorBase);
}
$(inicia);
However, I wrote the last line before it happened, and I wrote it in another way
inicia();
I wanted to know what the difference was, and why, of my code worked, even after the teacher said it was wrong, and that it should not run.