Error in javascript code

Asked

Viewed 36 times

-1

I’m having an error in my course exercise someone can help me?

(function(){
        var mes = prompt("Deseja calcular quantos meses? [0 à 3]");
        mes = parseFloat(mes);
        if(mes === 1){
            function mes1(P,I){
                montante1 = 'S = ' + P(1 + I);
                return montante;
            }

        }
        else if(mes === 2){
            function mes2(P,I){
                montante2 = 'S = ' + P(1 + I)*(1 + I);
                return montante;

            }
        }
         else if(mes === 3){
             function mes3(P,I){
                 montante3 = 'S = ' + P(1 + I)*(1 + I)*(1 + I);
                 return montante;
             }
         }
         var P = prompt("Digite a principal: ");
         var I = prompt("Digite a taxa de juros: ");
         var montantemes1 = mes1(P,I);
         var montantemes2 = mes2(P,I);
         var montantemes3 = mes3(P,I);
            document.write(montantemes1);
            document.write(montantemes2);
            document.write(montantemes3);
    })()

inserir a descrição da imagem aqui

  • What error? Could you elaborate a [mcve]? And [Edit] the question describing in words what the code should do? You define functions within conditionals, but call them independent of the condition, which makes no sense; if mes value 1, only the function mes1 will be defined, but even so you call at the end the functions mes2 and mes3. Why?

  • The error is the following appears the Alert but it does not show the result so the mes2 and mes3 it is the other calculation of interest if the user type 2 or 3. Can I assign the same condition in three or more functions as I did?? if not I already know that the problem was this but if yes the problem is another! Thanks for the attention

1 answer

1


See if that’s what you’d like to do:

(function() {
  var montante1 = null;
  var montante2 = null;
  var montante3 = null;

  var mes = prompt("Deseja calcular quantos meses? [0 à 3]");
  mes = parseFloat(mes);

  var P = prompt("Digite a principal: ");
  var I = prompt("Digite a taxa de juros: ");

  if (mes === 1) {
    montante1 = 'S = ' + P * (1 + I);
  } else if (mes === 2) {
    montante2 = 'S = ' + P * (1 + I) * (1 + I);
  } else if (mes === 3) {
    montante3 = 'S = ' + P* (1 + I) * (1 + I) * (1 + I);
  }

  if(montante1){
    document.write(montante1);
  }
  
  if(montante2){  
    document.write(montante2);
  }
  
  if(montante3){
    document.write(montante3);
  }
  
})()

Tips:

  • Thank you very much Thank you! That’s right thank you

Browser other questions tagged

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