Question

I have a series of two methods in which the second method must use the return value of the first. So in my code, as shown below, totalRoute needs the value returned from totalInvoiced( which is value3) to calculate its value, which will then be displayed in the register I am building. totalRoute will be equal to totalInvoiced minus the total sum of notCollected, notCollectedLate, expenditure1 and expenditure2.

My problme is that when I insert the values for notCollected, notCollectedLate, expenditure1 and expenditure2 the value of totalRoute is NaN. I suppose that is because I am not using the returned value from totalInvoiced correctley or the value is not being passed correctly. Why is my function not getting or using the value from totalInvoiced?

My fiddle here

My JS code here:

var A = {
    today: document.getElementById("today"),

    displayDate: function () {
        var cD = new Date();
        var day = cD.getDate();
        var months = ["Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sept", "Oct", "Nov", "Dec"];
        var year = cD.getFullYear();
        var today1 = A.today;

        today1.innerHTML = (day + "/" + months[cD.getMonth()] + "/" + year);
    },

    invoiced: document.getElementById("invoiced"),

    lastInvoiced: document.getElementById("lastinvoiced"),

    totalInvoiced: function () {

        var value1 = parseFloat(A.invoiced.value);

        var value2 = parseFloat(A.lastInvoiced.value);

        if (isNaN(value1))
            value1 = 0;
        if (isNaN(value2))
            value2 = 0;

        var totalInvoiced1 = value1 + value2;
        var value3 = document.getElementById("daytotal").value = totalInvoiced1 + "€";
        console.log(value3);
        return value3;
    },

    notCollected: document.getElementById("notcollected"),

    notCollectedLate: document.getElementById("notcollectedlate"),

    expenditure1: document.getElementById("expenditure1"),

    expenditure2: document.getElementById("expenditure2"),

    totalRoute: function () {
        var value4 = parseFloat(A.notCollected.value);
        var value5 = parseFloat(A.notCollectedLate.value);
        var value6 = parseFloat(A.expenditure1.value);
        var value7 = parseFloat(A.expenditure2.value);

        if (isNaN(value4))
            value4 = 0;
        if (isNaN(value5))
            value5 = 0;
        if (isNaN(value6))
            value6 = 0;
        if (isNaN(value7))
            value7 = 0;

        var totalExp = (value4 + value5 + value6 + value7);
        var value3 = A.totalInvoiced();
        var tRoute = value3 - totalExp;
        var value8 = document.getElementById("total").value = tRoute + "€";

        console.log(value8);
        return value8;
    }


};
window.onload = A.totalInvoiced();
window.onload = A.displayDate();

A.invoiced.addEventListener("change", A.totalInvoiced, false);
A.lastInvoiced.addEventListener("change", A.totalInvoiced, false);

A.notCollected.addEventListener("change", A.totalRoute, false);
A.notCollectedLate.addEventListener("change", A.totalRoute, false);
A.expenditure1.addEventListener("change", A.totalRoute, false);
A.expenditure2.addEventListener("change", A.totalRoute, false);
Était-ce utile?

La solution

value3 returned in totalInvoiced includes the euro character. Just change the return of totalInvoiced to totalInvoiced1 instead of value3 and that'll get the functionality that I think you're looking for.

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top