Javascript functions: One function inside the other

Asked

Viewed 678 times

2

1 - I have to create two functions inside each other.
2 - The first function has two parameters.
3 - The second function has a parameter.
4 - I followed the problem statement:
5 - Declare a function called threefold() that takes two parameters. Then you have to add both and return three times the result value of the sum of the two parameters.

To do this, you already count (even if you don’t see it declared) with the triple function, which takes a parameter and returns its value multiplied by three. 6 - I did it that way:

function triploDaSoma (num1,num2) funcaotriplo(num3) {
  funcaotriplo = triploDaSoma (num1+num2);
  return funcaotriplo * 3;
}
triploDaSoma (3,2);

7 - This is the error that appears:

Unexpected Identifier

  • 1

    See if that help you.

2 answers

1

Your problem can be solved with one of the solutions below

/*
*Crio a função com três parametros 
*sendo que num3 sera o mutiplicador
*num1 e num2 serão somados
*/
function triploDaSoma(num1,num2, num3){
  let soma = num1+num2;
  //a função triplo retorna o triplo e esta só existira aqui
  function triplo(){
    //aqui é realizada a multiplicação
    //Repare que o return só vale para a função triplo
    return soma*num3
  }
  //retorno o resultado da função triploDaSoma
  return triplo();
}

console.log(triploDaSoma(3,2, 3));
//->15 //ja que 3+2==5*3==15

If I may, I would write this piece of code as follows

/*
*Renomeio a função para espreçar o que realmente ela esta fazendo
*/
function soma(num1,num2){
  //variavel que guardará o seu resultado
  let soma = num1+num2;
  //não recebe parametro já que seu objetivo é triplicar. mais nada impede
  function triplo(){
    return soma*3;
  }
  
  return {
    soma,
    triplo
  }
}
var somaDeFora = soma(3,2);
//então se quiser a soma
console.log(somaDeFora.soma);
//->5

//e se quiser o triplo
console.log(somaDeFora.triplo());
//->15

I hope I helped hug...

1


First of all yes you can declare in javascript one function within the other.

That kind of statement is called Nested Function and is initially accessible only to the scope containing it, but its reference can be returned to another scope.

Is a closure because it carries with it the scope to which it was created, even if its symbols are not used.

And it is declared as a normal function only within another function.

//Função encapsuladora que retora o triplo da some de dois números.
function triploDaSoma(a, b) {
  //Função aninhada que retorna o triplo de um número.
  function triplo(x) {
    return 3 * x;
  }
  //Retorna o triplo da soma de a com b usando a função aninhada.
  return triplo(a + b);
}

console.log(triploDaSoma(3, 2));

But this code only serves as an exercise because it does exactly the same as this simplest:

function triploDaSoma(a, b) {
  return 3 * (a + b);
}
console.log(triploDaSoma(3, 2));

  • Hello Augusto, your logical reasoning solved the problem. I made some small adjustments: type (triple) and instead of (a,b) (x) I used (num1,num2) (num3) for me it is easier so kkkk. THANK YOU !!!

Browser other questions tagged

You are not signed in. Login or sign up in order to post.