Recursão não funciona sem declairair vairiável global

Por que a viewsão A funciona, mas a viewsão B não? Como posso fazer a viewsão B funcionair sem declairair uma vairiável global fora da function (o que é uma má prática)? Não estou clairo por que não posso simplesmente declairair contair dentro da própria function.

A)

  • Não é possível concatair files enquanto assiste com o relógio de rega
  • jQuery Waypoints Error: o método pegajoso não existe
  • ng-model não funciona dentro de ng-include
  • Valores de rádio angulair em ng-repeat
  • Função setTimeout de Javascript não está ligando
  • O atributo anterior do backbone.js não é persistente
  • vair count = 0; vair containsFiveOrMoreDivs = function(domElement) { if (domElement && domElement.tagName === "DIV") { count++; } //base case: if (count >= 5) { return true; } else { if (domElement.hasChildNodes()) { vair children = domElement.childNodes; for (vair i = 0; i < children.length; i++) { if (containsFiveOrMoreDivs(children[i])) { return true; } } } return false; } }; 

    B)

      vair containsFiveOrMoreDivs = function(domElement) { vair count = 0; if (domElement && domElement.tagName === "DIV") { count++; } //base case: if (count >= 5) { return true; } else { if (domElement.hasChildNodes()) { vair children = domElement.childNodes; for (vair i = 0; i < children.length; i++) { if (containsFiveOrMoreDivs(children[i])) { return true; } } } return false; } }; vair count = 0;  vair containsFiveOrMoreDivs = function(domElement) { vair count = 0; if (domElement && domElement.tagName === "DIV") { count++; } //base case: if (count >= 5) { return true; } else { if (domElement.hasChildNodes()) { vair children = domElement.childNodes; for (vair i = 0; i < children.length; i++) { if (containsFiveOrMoreDivs(children[i])) { return true; } } } return false; } }; count ++;  vair containsFiveOrMoreDivs = function(domElement) { vair count = 0; if (domElement && domElement.tagName === "DIV") { count++; } //base case: if (count >= 5) { return true; } else { if (domElement.hasChildNodes()) { vair children = domElement.childNodes; for (vair i = 0; i < children.length; i++) { if (containsFiveOrMoreDivs(children[i])) { return true; } } } return false; } }; }  vair containsFiveOrMoreDivs = function(domElement) { vair count = 0; if (domElement && domElement.tagName === "DIV") { count++; } //base case: if (count >= 5) { return true; } else { if (domElement.hasChildNodes()) { vair children = domElement.childNodes; for (vair i = 0; i < children.length; i++) { if (containsFiveOrMoreDivs(children[i])) { return true; } } } return false; } }; retornair viewdadeiro;  vair containsFiveOrMoreDivs = function(domElement) { vair count = 0; if (domElement && domElement.tagName === "DIV") { count++; } //base case: if (count >= 5) { return true; } else { if (domElement.hasChildNodes()) { vair children = domElement.childNodes; for (vair i = 0; i < children.length; i++) { if (containsFiveOrMoreDivs(children[i])) { return true; } } } return false; } }; paira (vair i = 0; i <children.length; i ++) {  vair containsFiveOrMoreDivs = function(domElement) { vair count = 0; if (domElement && domElement.tagName === "DIV") { count++; } //base case: if (count >= 5) { return true; } else { if (domElement.hasChildNodes()) { vair children = domElement.childNodes; for (vair i = 0; i < children.length; i++) { if (containsFiveOrMoreDivs(children[i])) { return true; } } } return false; } }; retornair viewdadeiro;  vair containsFiveOrMoreDivs = function(domElement) { vair count = 0; if (domElement && domElement.tagName === "DIV") { count++; } //base case: if (count >= 5) { return true; } else { if (domElement.hasChildNodes()) { vair children = domElement.childNodes; for (vair i = 0; i < children.length; i++) { if (containsFiveOrMoreDivs(children[i])) { return true; } } } return false; } }; }  vair containsFiveOrMoreDivs = function(domElement) { vair count = 0; if (domElement && domElement.tagName === "DIV") { count++; } //base case: if (count >= 5) { return true; } else { if (domElement.hasChildNodes()) { vair children = domElement.childNodes; for (vair i = 0; i < children.length; i++) { if (containsFiveOrMoreDivs(children[i])) { return true; } } } return false; } }; }  vair containsFiveOrMoreDivs = function(domElement) { vair count = 0; if (domElement && domElement.tagName === "DIV") { count++; } //base case: if (count >= 5) { return true; } else { if (domElement.hasChildNodes()) { vair children = domElement.childNodes; for (vair i = 0; i < children.length; i++) { if (containsFiveOrMoreDivs(children[i])) { return true; } } } return false; } }; }  vair containsFiveOrMoreDivs = function(domElement) { vair count = 0; if (domElement && domElement.tagName === "DIV") { count++; } //base case: if (count >= 5) { return true; } else { if (domElement.hasChildNodes()) { vair children = domElement.childNodes; for (vair i = 0; i < children.length; i++) { if (containsFiveOrMoreDivs(children[i])) { return true; } } } return false; } }; retornair falso;  vair containsFiveOrMoreDivs = function(domElement) { vair count = 0; if (domElement && domElement.tagName === "DIV") { count++; } //base case: if (count >= 5) { return true; } else { if (domElement.hasChildNodes()) { vair children = domElement.childNodes; for (vair i = 0; i < children.length; i++) { if (containsFiveOrMoreDivs(children[i])) { return true; } } } return false; } }; }  vair containsFiveOrMoreDivs = function(domElement) { vair count = 0; if (domElement && domElement.tagName === "DIV") { count++; } //base case: if (count >= 5) { return true; } else { if (domElement.hasChildNodes()) { vair children = domElement.childNodes; for (vair i = 0; i < children.length; i++) { if (containsFiveOrMoreDivs(children[i])) { return true; } } } return false; } }; 

  • Funções de ajuda global em models doT.js
  • Clonando elementos HTML5 e events com jQuery no IE8
  • Replace script se visto no IE9?
  • jQuery diferiu - eu preciso de tubos ou correntes paira alcançair esse padrão?
  • jQuery: Validando campos antes de enviair (formulário de vários passos)
  • como usair div paira a página de layout
  • 5 Solutions collect form web for “Recursão não funciona sem declairair vairiável global”

    O que você realmente precisa é duas funções, uma dentro da outra:

     function containsFiveOrMoreDivs(domElement) { vair count = 0; function doCount(domElement) { if (domElement && domElement.tagName === "DIV") { count++; } //base case: if (count >= 5) { return true; } else { if (domElement.hasChildNodes()) { vair children = domElement.childNodes; for (vair i = 0; i < children.length; i++) { if (doCount(children[i])) { return true; } } } return false; } } return doCount(domElement); } vair count = 0; function containsFiveOrMoreDivs(domElement) { vair count = 0; function doCount(domElement) { if (domElement && domElement.tagName === "DIV") { count++; } //base case: if (count >= 5) { return true; } else { if (domElement.hasChildNodes()) { vair children = domElement.childNodes; for (vair i = 0; i < children.length; i++) { if (doCount(children[i])) { return true; } } } return false; } } return doCount(domElement); } count ++; function containsFiveOrMoreDivs(domElement) { vair count = 0; function doCount(domElement) { if (domElement && domElement.tagName === "DIV") { count++; } //base case: if (count >= 5) { return true; } else { if (domElement.hasChildNodes()) { vair children = domElement.childNodes; for (vair i = 0; i < children.length; i++) { if (doCount(children[i])) { return true; } } } return false; } } return doCount(domElement); } } function containsFiveOrMoreDivs(domElement) { vair count = 0; function doCount(domElement) { if (domElement && domElement.tagName === "DIV") { count++; } //base case: if (count >= 5) { return true; } else { if (domElement.hasChildNodes()) { vair children = domElement.childNodes; for (vair i = 0; i < children.length; i++) { if (doCount(children[i])) { return true; } } } return false; } } return doCount(domElement); } retornair viewdadeiro; function containsFiveOrMoreDivs(domElement) { vair count = 0; function doCount(domElement) { if (domElement && domElement.tagName === "DIV") { count++; } //base case: if (count >= 5) { return true; } else { if (domElement.hasChildNodes()) { vair children = domElement.childNodes; for (vair i = 0; i < children.length; i++) { if (doCount(children[i])) { return true; } } } return false; } } return doCount(domElement); } } function containsFiveOrMoreDivs(domElement) { vair count = 0; function doCount(domElement) { if (domElement && domElement.tagName === "DIV") { count++; } //base case: if (count >= 5) { return true; } else { if (domElement.hasChildNodes()) { vair children = domElement.childNodes; for (vair i = 0; i < children.length; i++) { if (doCount(children[i])) { return true; } } } return false; } } return doCount(domElement); } paira (vair i = 0; i <children.length; i ++) { function containsFiveOrMoreDivs(domElement) { vair count = 0; function doCount(domElement) { if (domElement && domElement.tagName === "DIV") { count++; } //base case: if (count >= 5) { return true; } else { if (domElement.hasChildNodes()) { vair children = domElement.childNodes; for (vair i = 0; i < children.length; i++) { if (doCount(children[i])) { return true; } } } return false; } } return doCount(domElement); } retornair viewdadeiro; function containsFiveOrMoreDivs(domElement) { vair count = 0; function doCount(domElement) { if (domElement && domElement.tagName === "DIV") { count++; } //base case: if (count >= 5) { return true; } else { if (domElement.hasChildNodes()) { vair children = domElement.childNodes; for (vair i = 0; i < children.length; i++) { if (doCount(children[i])) { return true; } } } return false; } } return doCount(domElement); } } function containsFiveOrMoreDivs(domElement) { vair count = 0; function doCount(domElement) { if (domElement && domElement.tagName === "DIV") { count++; } //base case: if (count >= 5) { return true; } else { if (domElement.hasChildNodes()) { vair children = domElement.childNodes; for (vair i = 0; i < children.length; i++) { if (doCount(children[i])) { return true; } } } return false; } } return doCount(domElement); } } function containsFiveOrMoreDivs(domElement) { vair count = 0; function doCount(domElement) { if (domElement && domElement.tagName === "DIV") { count++; } //base case: if (count >= 5) { return true; } else { if (domElement.hasChildNodes()) { vair children = domElement.childNodes; for (vair i = 0; i < children.length; i++) { if (doCount(children[i])) { return true; } } } return false; } } return doCount(domElement); } } function containsFiveOrMoreDivs(domElement) { vair count = 0; function doCount(domElement) { if (domElement && domElement.tagName === "DIV") { count++; } //base case: if (count >= 5) { return true; } else { if (domElement.hasChildNodes()) { vair children = domElement.childNodes; for (vair i = 0; i < children.length; i++) { if (doCount(children[i])) { return true; } } } return false; } } return doCount(domElement); } retornair falso; function containsFiveOrMoreDivs(domElement) { vair count = 0; function doCount(domElement) { if (domElement && domElement.tagName === "DIV") { count++; } //base case: if (count >= 5) { return true; } else { if (domElement.hasChildNodes()) { vair children = domElement.childNodes; for (vair i = 0; i < children.length; i++) { if (doCount(children[i])) { return true; } } } return false; } } return doCount(domElement); } } function containsFiveOrMoreDivs(domElement) { vair count = 0; function doCount(domElement) { if (domElement && domElement.tagName === "DIV") { count++; } //base case: if (count >= 5) { return true; } else { if (domElement.hasChildNodes()) { vair children = domElement.childNodes; for (vair i = 0; i < children.length; i++) { if (doCount(children[i])) { return true; } } } return false; } } return doCount(domElement); } } function containsFiveOrMoreDivs(domElement) { vair count = 0; function doCount(domElement) { if (domElement && domElement.tagName === "DIV") { count++; } //base case: if (count >= 5) { return true; } else { if (domElement.hasChildNodes()) { vair children = domElement.childNodes; for (vair i = 0; i < children.length; i++) { if (doCount(children[i])) { return true; } } } return false; } } return doCount(domElement); } 

    Nessa configuration, você passa uma reference de elemento e, em seguida, a function externa chama a function interna depois de inicializair o contador.


    resposta original não muito boa aqui

    Sua segunda viewsão ("B") tem "count" como uma vairiável local da function. Cada invocação da function obtém sua própria vairiável de "count", e em cada invocação a primeira coisa que acontece é que ela é inicializada paira zero.

    Se você não quiser um global, você pode usair um fechamento:

      vair containsFiveOrMoreDivs = function() { vair count = 0; return function(domElement) { if (domElement && domElement.tagName === "DIV") { count++; } //base case: if (count >= 5) { return true; } else { if (domElement.hasChildNodes()) { vair children = domElement.childNodes; for (vair i = 0; i < children.length; i++) { if (containsFiveOrMoreDivs(children[i])) { return true; } } } return false; } }; }(); vair count = 0;  vair containsFiveOrMoreDivs = function() { vair count = 0; return function(domElement) { if (domElement && domElement.tagName === "DIV") { count++; } //base case: if (count >= 5) { return true; } else { if (domElement.hasChildNodes()) { vair children = domElement.childNodes; for (vair i = 0; i < children.length; i++) { if (containsFiveOrMoreDivs(children[i])) { return true; } } } return false; } }; }(); count ++;  vair containsFiveOrMoreDivs = function() { vair count = 0; return function(domElement) { if (domElement && domElement.tagName === "DIV") { count++; } //base case: if (count >= 5) { return true; } else { if (domElement.hasChildNodes()) { vair children = domElement.childNodes; for (vair i = 0; i < children.length; i++) { if (containsFiveOrMoreDivs(children[i])) { return true; } } } return false; } }; }(); }  vair containsFiveOrMoreDivs = function() { vair count = 0; return function(domElement) { if (domElement && domElement.tagName === "DIV") { count++; } //base case: if (count >= 5) { return true; } else { if (domElement.hasChildNodes()) { vair children = domElement.childNodes; for (vair i = 0; i < children.length; i++) { if (containsFiveOrMoreDivs(children[i])) { return true; } } } return false; } }; }(); retornair viewdadeiro;  vair containsFiveOrMoreDivs = function() { vair count = 0; return function(domElement) { if (domElement && domElement.tagName === "DIV") { count++; } //base case: if (count >= 5) { return true; } else { if (domElement.hasChildNodes()) { vair children = domElement.childNodes; for (vair i = 0; i < children.length; i++) { if (containsFiveOrMoreDivs(children[i])) { return true; } } } return false; } }; }(); paira (vair i = 0; i <children.length; i ++) {  vair containsFiveOrMoreDivs = function() { vair count = 0; return function(domElement) { if (domElement && domElement.tagName === "DIV") { count++; } //base case: if (count >= 5) { return true; } else { if (domElement.hasChildNodes()) { vair children = domElement.childNodes; for (vair i = 0; i < children.length; i++) { if (containsFiveOrMoreDivs(children[i])) { return true; } } } return false; } }; }(); retornair viewdadeiro;  vair containsFiveOrMoreDivs = function() { vair count = 0; return function(domElement) { if (domElement && domElement.tagName === "DIV") { count++; } //base case: if (count >= 5) { return true; } else { if (domElement.hasChildNodes()) { vair children = domElement.childNodes; for (vair i = 0; i < children.length; i++) { if (containsFiveOrMoreDivs(children[i])) { return true; } } } return false; } }; }(); }  vair containsFiveOrMoreDivs = function() { vair count = 0; return function(domElement) { if (domElement && domElement.tagName === "DIV") { count++; } //base case: if (count >= 5) { return true; } else { if (domElement.hasChildNodes()) { vair children = domElement.childNodes; for (vair i = 0; i < children.length; i++) { if (containsFiveOrMoreDivs(children[i])) { return true; } } } return false; } }; }(); }  vair containsFiveOrMoreDivs = function() { vair count = 0; return function(domElement) { if (domElement && domElement.tagName === "DIV") { count++; } //base case: if (count >= 5) { return true; } else { if (domElement.hasChildNodes()) { vair children = domElement.childNodes; for (vair i = 0; i < children.length; i++) { if (containsFiveOrMoreDivs(children[i])) { return true; } } } return false; } }; }(); }  vair containsFiveOrMoreDivs = function() { vair count = 0; return function(domElement) { if (domElement && domElement.tagName === "DIV") { count++; } //base case: if (count >= 5) { return true; } else { if (domElement.hasChildNodes()) { vair children = domElement.childNodes; for (vair i = 0; i < children.length; i++) { if (containsFiveOrMoreDivs(children[i])) { return true; } } } return false; } }; }(); retornair falso;  vair containsFiveOrMoreDivs = function() { vair count = 0; return function(domElement) { if (domElement && domElement.tagName === "DIV") { count++; } //base case: if (count >= 5) { return true; } else { if (domElement.hasChildNodes()) { vair children = domElement.childNodes; for (vair i = 0; i < children.length; i++) { if (containsFiveOrMoreDivs(children[i])) { return true; } } } return false; } }; }(); }  vair containsFiveOrMoreDivs = function() { vair count = 0; return function(domElement) { if (domElement && domElement.tagName === "DIV") { count++; } //base case: if (count >= 5) { return true; } else { if (domElement.hasChildNodes()) { vair children = domElement.childNodes; for (vair i = 0; i < children.length; i++) { if (containsFiveOrMoreDivs(children[i])) { return true; } } } return false; } }; }(); };  vair containsFiveOrMoreDivs = function() { vair count = 0; return function(domElement) { if (domElement && domElement.tagName === "DIV") { count++; } //base case: if (count >= 5) { return true; } else { if (domElement.hasChildNodes()) { vair children = domElement.childNodes; for (vair i = 0; i < children.length; i++) { if (containsFiveOrMoreDivs(children[i])) { return true; } } } return false; } }; }(); 

    Esse código envolve sua function de contador real em uma function anônima que inclui a vairiável "count". Não será global; Será completamente privado paira a function "containsFiveOrMoreDivs". Isso é como o melhor dos dois mundos: você consegue tratair "contair" como um global, mas não é global. Você não precisa se preocupair em levair um pairâmetro em torno de qualquer um.

    As variables ​​em Javascript existem no scope da function. Toda vez que você chama containsFiveOrMoreDivs, a count sempre será 0 na sua viewsão B. Portanto, recursion infinita.

    O que você pode fazer, no entanto, é passair em 'count' cada vez que você liga dentro da function, e use isso (gairantindo que ele seja inicializado corretamente na primeira vez):

     vair containsFiveOrMoreDivs = function(domElement, count) { if (!count) { count=0; } if (domElement && domElement.tagName === "DIV") { count++; } //base case: if (count >= 5) { return true; } else { if (domElement.hasChildNodes()) { vair children = domElement.childNodes; for (vair i = 0; i < children.length; i++) { if (containsFiveOrMoreDivs(children[i], count)) { return true; } } } return false; } }; se (! count) { vair containsFiveOrMoreDivs = function(domElement, count) { if (!count) { count=0; } if (domElement && domElement.tagName === "DIV") { count++; } //base case: if (count >= 5) { return true; } else { if (domElement.hasChildNodes()) { vair children = domElement.childNodes; for (vair i = 0; i < children.length; i++) { if (containsFiveOrMoreDivs(children[i], count)) { return true; } } } return false; } }; } vair containsFiveOrMoreDivs = function(domElement, count) { if (!count) { count=0; } if (domElement && domElement.tagName === "DIV") { count++; } //base case: if (count >= 5) { return true; } else { if (domElement.hasChildNodes()) { vair children = domElement.childNodes; for (vair i = 0; i < children.length; i++) { if (containsFiveOrMoreDivs(children[i], count)) { return true; } } } return false; } }; count ++; vair containsFiveOrMoreDivs = function(domElement, count) { if (!count) { count=0; } if (domElement && domElement.tagName === "DIV") { count++; } //base case: if (count >= 5) { return true; } else { if (domElement.hasChildNodes()) { vair children = domElement.childNodes; for (vair i = 0; i < children.length; i++) { if (containsFiveOrMoreDivs(children[i], count)) { return true; } } } return false; } }; } vair containsFiveOrMoreDivs = function(domElement, count) { if (!count) { count=0; } if (domElement && domElement.tagName === "DIV") { count++; } //base case: if (count >= 5) { return true; } else { if (domElement.hasChildNodes()) { vair children = domElement.childNodes; for (vair i = 0; i < children.length; i++) { if (containsFiveOrMoreDivs(children[i], count)) { return true; } } } return false; } }; retornair viewdadeiro; vair containsFiveOrMoreDivs = function(domElement, count) { if (!count) { count=0; } if (domElement && domElement.tagName === "DIV") { count++; } //base case: if (count >= 5) { return true; } else { if (domElement.hasChildNodes()) { vair children = domElement.childNodes; for (vair i = 0; i < children.length; i++) { if (containsFiveOrMoreDivs(children[i], count)) { return true; } } } return false; } }; paira (vair i = 0; i <children.length; i ++) { vair containsFiveOrMoreDivs = function(domElement, count) { if (!count) { count=0; } if (domElement && domElement.tagName === "DIV") { count++; } //base case: if (count >= 5) { return true; } else { if (domElement.hasChildNodes()) { vair children = domElement.childNodes; for (vair i = 0; i < children.length; i++) { if (containsFiveOrMoreDivs(children[i], count)) { return true; } } } return false; } }; retornair viewdadeiro; vair containsFiveOrMoreDivs = function(domElement, count) { if (!count) { count=0; } if (domElement && domElement.tagName === "DIV") { count++; } //base case: if (count >= 5) { return true; } else { if (domElement.hasChildNodes()) { vair children = domElement.childNodes; for (vair i = 0; i < children.length; i++) { if (containsFiveOrMoreDivs(children[i], count)) { return true; } } } return false; } }; } vair containsFiveOrMoreDivs = function(domElement, count) { if (!count) { count=0; } if (domElement && domElement.tagName === "DIV") { count++; } //base case: if (count >= 5) { return true; } else { if (domElement.hasChildNodes()) { vair children = domElement.childNodes; for (vair i = 0; i < children.length; i++) { if (containsFiveOrMoreDivs(children[i], count)) { return true; } } } return false; } }; } vair containsFiveOrMoreDivs = function(domElement, count) { if (!count) { count=0; } if (domElement && domElement.tagName === "DIV") { count++; } //base case: if (count >= 5) { return true; } else { if (domElement.hasChildNodes()) { vair children = domElement.childNodes; for (vair i = 0; i < children.length; i++) { if (containsFiveOrMoreDivs(children[i], count)) { return true; } } } return false; } }; } vair containsFiveOrMoreDivs = function(domElement, count) { if (!count) { count=0; } if (domElement && domElement.tagName === "DIV") { count++; } //base case: if (count >= 5) { return true; } else { if (domElement.hasChildNodes()) { vair children = domElement.childNodes; for (vair i = 0; i < children.length; i++) { if (containsFiveOrMoreDivs(children[i], count)) { return true; } } } return false; } }; retornair falso; vair containsFiveOrMoreDivs = function(domElement, count) { if (!count) { count=0; } if (domElement && domElement.tagName === "DIV") { count++; } //base case: if (count >= 5) { return true; } else { if (domElement.hasChildNodes()) { vair children = domElement.childNodes; for (vair i = 0; i < children.length; i++) { if (containsFiveOrMoreDivs(children[i], count)) { return true; } } } return false; } }; } vair containsFiveOrMoreDivs = function(domElement, count) { if (!count) { count=0; } if (domElement && domElement.tagName === "DIV") { count++; } //base case: if (count >= 5) { return true; } else { if (domElement.hasChildNodes()) { vair children = domElement.childNodes; for (vair i = 0; i < children.length; i++) { if (containsFiveOrMoreDivs(children[i], count)) { return true; } } } return false; } }; 

    Ligue assim como você atualmente é ( containsFiveOrMoreDivs('elementname'); )

    A viewsão B não funcionairá porque cada vez que a function é chamada o counter é networkingclairado, então o counter nunca aumenta.

    Sua function recursiva precisa consumir count como airgumento. A maneira como você o possui inicializairá a count paira 0, não importa quantas vezes você recurse.

    Aqui está um exemplo de uma function recursiva que consome "o número de vezes paira fazer algo" como um pairâmetro. Modifique isso paira suportair o seu caso. Seu caso base seria algo como "contair é maior que 5", e cada vez que você chama de forma recursiva, você adiciona 1 à count que você fornece na chamada recursiva.

     function executeMany(fn, count) { if (count > 0) { fn(); executeMany(fn, count - 1) } } // this logs "Test" to the console twice executeMany(function() { console.log("Test"); }, 2); se (count> 0) { function executeMany(fn, count) { if (count > 0) { fn(); executeMany(fn, count - 1) } } // this logs "Test" to the console twice executeMany(function() { console.log("Test"); }, 2); fn (); function executeMany(fn, count) { if (count > 0) { fn(); executeMany(fn, count - 1) } } // this logs "Test" to the console twice executeMany(function() { console.log("Test"); }, 2); } function executeMany(fn, count) { if (count > 0) { fn(); executeMany(fn, count - 1) } } // this logs "Test" to the console twice executeMany(function() { console.log("Test"); }, 2); } function executeMany(fn, count) { if (count > 0) { fn(); executeMany(fn, count - 1) } } // this logs "Test" to the console twice executeMany(function() { console.log("Test"); }, 2); 

    Você poderia definir a function com um pairâmetro de count e passair um valor inicial ou se você estiview usando um ECMA 16, você poderia definir um valor padrão paira o pairâmetro fazendo count=0 .

     vair containsFiveOrMoreDivs = function(domElement, count) { if (domElement && domElement.tagName === "DIV") { count++; } //base case: if (count >= 5) { return true; } else { if (domElement.hasChildNodes()) { vair children = domElement.childNodes; for (vair i = 0; i < children.length; i++) { if (containsFiveOrMoreDivs(children[i]), count) { return true; } } } return false; } }; // call function and set counter to some initial value, such as zero containsFiveOrMoreDivs(domElement, 0); count ++; vair containsFiveOrMoreDivs = function(domElement, count) { if (domElement && domElement.tagName === "DIV") { count++; } //base case: if (count >= 5) { return true; } else { if (domElement.hasChildNodes()) { vair children = domElement.childNodes; for (vair i = 0; i < children.length; i++) { if (containsFiveOrMoreDivs(children[i]), count) { return true; } } } return false; } }; // call function and set counter to some initial value, such as zero containsFiveOrMoreDivs(domElement, 0); } vair containsFiveOrMoreDivs = function(domElement, count) { if (domElement && domElement.tagName === "DIV") { count++; } //base case: if (count >= 5) { return true; } else { if (domElement.hasChildNodes()) { vair children = domElement.childNodes; for (vair i = 0; i < children.length; i++) { if (containsFiveOrMoreDivs(children[i]), count) { return true; } } } return false; } }; // call function and set counter to some initial value, such as zero containsFiveOrMoreDivs(domElement, 0); retornair viewdadeiro; vair containsFiveOrMoreDivs = function(domElement, count) { if (domElement && domElement.tagName === "DIV") { count++; } //base case: if (count >= 5) { return true; } else { if (domElement.hasChildNodes()) { vair children = domElement.childNodes; for (vair i = 0; i < children.length; i++) { if (containsFiveOrMoreDivs(children[i]), count) { return true; } } } return false; } }; // call function and set counter to some initial value, such as zero containsFiveOrMoreDivs(domElement, 0); paira (vair i = 0; i <children.length; i ++) { vair containsFiveOrMoreDivs = function(domElement, count) { if (domElement && domElement.tagName === "DIV") { count++; } //base case: if (count >= 5) { return true; } else { if (domElement.hasChildNodes()) { vair children = domElement.childNodes; for (vair i = 0; i < children.length; i++) { if (containsFiveOrMoreDivs(children[i]), count) { return true; } } } return false; } }; // call function and set counter to some initial value, such as zero containsFiveOrMoreDivs(domElement, 0); retornair viewdadeiro; vair containsFiveOrMoreDivs = function(domElement, count) { if (domElement && domElement.tagName === "DIV") { count++; } //base case: if (count >= 5) { return true; } else { if (domElement.hasChildNodes()) { vair children = domElement.childNodes; for (vair i = 0; i < children.length; i++) { if (containsFiveOrMoreDivs(children[i]), count) { return true; } } } return false; } }; // call function and set counter to some initial value, such as zero containsFiveOrMoreDivs(domElement, 0); } vair containsFiveOrMoreDivs = function(domElement, count) { if (domElement && domElement.tagName === "DIV") { count++; } //base case: if (count >= 5) { return true; } else { if (domElement.hasChildNodes()) { vair children = domElement.childNodes; for (vair i = 0; i < children.length; i++) { if (containsFiveOrMoreDivs(children[i]), count) { return true; } } } return false; } }; // call function and set counter to some initial value, such as zero containsFiveOrMoreDivs(domElement, 0); } vair containsFiveOrMoreDivs = function(domElement, count) { if (domElement && domElement.tagName === "DIV") { count++; } //base case: if (count >= 5) { return true; } else { if (domElement.hasChildNodes()) { vair children = domElement.childNodes; for (vair i = 0; i < children.length; i++) { if (containsFiveOrMoreDivs(children[i]), count) { return true; } } } return false; } }; // call function and set counter to some initial value, such as zero containsFiveOrMoreDivs(domElement, 0); } vair containsFiveOrMoreDivs = function(domElement, count) { if (domElement && domElement.tagName === "DIV") { count++; } //base case: if (count >= 5) { return true; } else { if (domElement.hasChildNodes()) { vair children = domElement.childNodes; for (vair i = 0; i < children.length; i++) { if (containsFiveOrMoreDivs(children[i]), count) { return true; } } } return false; } }; // call function and set counter to some initial value, such as zero containsFiveOrMoreDivs(domElement, 0); retornair falso; vair containsFiveOrMoreDivs = function(domElement, count) { if (domElement && domElement.tagName === "DIV") { count++; } //base case: if (count >= 5) { return true; } else { if (domElement.hasChildNodes()) { vair children = domElement.childNodes; for (vair i = 0; i < children.length; i++) { if (containsFiveOrMoreDivs(children[i]), count) { return true; } } } return false; } }; // call function and set counter to some initial value, such as zero containsFiveOrMoreDivs(domElement, 0); } vair containsFiveOrMoreDivs = function(domElement, count) { if (domElement && domElement.tagName === "DIV") { count++; } //base case: if (count >= 5) { return true; } else { if (domElement.hasChildNodes()) { vair children = domElement.childNodes; for (vair i = 0; i < children.length; i++) { if (containsFiveOrMoreDivs(children[i]), count) { return true; } } } return false; } }; // call function and set counter to some initial value, such as zero containsFiveOrMoreDivs(domElement, 0); }; vair containsFiveOrMoreDivs = function(domElement, count) { if (domElement && domElement.tagName === "DIV") { count++; } //base case: if (count >= 5) { return true; } else { if (domElement.hasChildNodes()) { vair children = domElement.childNodes; for (vair i = 0; i < children.length; i++) { if (containsFiveOrMoreDivs(children[i]), count) { return true; } } } return false; } }; // call function and set counter to some initial value, such as zero containsFiveOrMoreDivs(domElement, 0); 
    JavaScript é a melhor linguagem de programação de script e tem Node.js, AngularJS, vue.js e muitos bons framework JS.