Function returning Nan failing to perform the calculation

Asked

Viewed 118 times

0

In a solar calculator, everything works, except the final value that returns Nan. Where is my error?

function calcular(valorInformado) {

    ResultPotencia = parseFloat(valorInformado * 8.28 / 1000).toFixed(2);

    ResultModulos = parseInt(ResultPotencia * 8.28 / 1000);

    ResultArea = parseFloat(ResultModulos * 2);

    Result1 = parseFloat(11 * ResultPotencia * 0.85 * 1000).toFixed(3);

    Result2 = parseFloat(11 * ResultPotencia * 1.10 * 1000).toFixed(3);



    var elemResult = document.getElementById("resultadopotencia");
    elemResult.innerText = " " + ResultPotencia + " KWp" ;

    var elemResult = document.getElementById("resultadomodulos");
    elemResult.innerText = " " + parseInt(ResultPotencia * 1000 / 265) + " Módulos"; 

    var elemResult = document.getElementById("resultadometros");
    elemResult.innerText = " " + (ResultPotencia * 1000 / 265 * 2).toFixed(1) + " M²"; 

    var elemResult = document.getElementById("valorsistema");
    elemResult.innerText = " " + (ResultIntermediario * ResultPotencia * 0.85 * 1000).toFixed(2).formatMoney();

    var elemResult = document.getElementById("valorsistema2");
    elemResult.innerText = " " + (ResultIntermediario * ResultPotencia * 1.10 * 1000).toFixed(2).formatMoney();

}

ResultIntermediario = function() {
    var ResultIntermediario = this;

    if(ResultPotencia <= 3)
    ResultIntermediario = 11;

    if(ResultPotencia >= 3)
    ResultIntermediario = 9;

}

1 answer

0


Well I managed to find the errors, I redid the code to debug it in the browser, you made a simple mistake in the function:

ResultIntermediario = function() {
var ResultIntermediario = this;

if(ResultPotencia <= 3)
ResultIntermediario = 11;

if(ResultPotencia >= 3)
ResultIntermediario = 9;}

You did not specify any parameter and no return, so at the time of calculation you get Nan, which is the reference to null. I modified it as follows:

ResultIntermediario = function(ResultPotencia) {

if(ResultPotencia <= 3) return 11 * ResultPotencia;

if(ResultPotencia >= 3) return 9 * ResultPotencia;}

Now it takes a parameter and returns a value * the Power Output (I saw that you had used the * power result function in Inner HTML and deduced this).

Follow the complete code, I tested it in my browser and everything worked, you just need to add the Inner HTML and the format again because I didn’t have these functions created.

function calcular(valorInformado) {

ResultPotencia = parseFloat(valorInformado * 8.28 / 1000).toFixed(2);

ResultModulos = parseInt(ResultPotencia * 8.28 / 1000);

ResultArea = parseFloat(ResultModulos * 2);

Result1 = parseFloat(11 * ResultPotencia * 0.85 * 1000).toFixed(3);

Result2 = parseFloat(11 * ResultPotencia * 1.10 * 1000).toFixed(3);

var elemResult = document.getElementById("resultadopotencia");
console.log(ResultPotencia + " KWp" );

var elemResult = document.getElementById("resultadomodulos");
console.log(parseInt(ResultPotencia * 1000 / 265) + " Módulos"); 

var elemResult = document.getElementById("resultadometros");
console.log((ResultPotencia * 1000 / 265 * 2).toFixed(1) + " M²"); 

var elemResult = document.getElementById("valorsistema");
console.log((ResultIntermediario(ResultPotencia) * 0.85 * 1000));

var elemResult = document.getElementById("valorsistema2");
console.log(ResultIntermediario(ResultPotencia) * 1.10 * 1000);}

Browser other questions tagged

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