Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

232
Vistas
why it reverse the opration after it do?

I try to add a numbers from 1 to 10 by JavaScript using the concept of recursion and it didn't work as intended. it revers the operation and it come up with the first result and when i use variable to store the result i get it come up with undefined. here is the code

var total = 0, count = 1;
function sum(total, count){
    if(count <= 3){
        total +=count;
        count++;
        sum(total, count);
    }else{
        return total;
    };
}
var result = sum(total, count);
console.log(result);
about 4 years ago · Juan Pablo Isaza
1 Respuestas
Responde la pregunta

0

You are not returning from your first if condition. Default return value for any function is undefined.

var total = 0, count = 1;
function sum(total, count){
    if(count <= 3){
        total +=count;
        count++;
        return sum(total, count);
    }else{
        return total;
    };
}
var result = sum(total, count);
console.log(result);

about 4 years ago · Juan Pablo Isaza Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda