jeudi 24 octobre 2019

$NaN Displaying instead of the total cost

So I have an assessment due tomorrow, I am almost complete however stuck on one last thing... My total cost summary. I have a booking summary and everything displays fine apart from the total cost, it just displays "$NaN"

I have looked up other examples but they are all basic (for example: var example = 4 + 4 + 5;)

Here is my main loop:

function loopForm(form) { // Main function that gathers inputs for booking summary aswell as firebase
    alert('Test alert for confirm booking'); // Test alert to see if function is working
    var amountOfDays = numberOfDays.value; // Defining the amount of days for the calculation(s)
    var insuranceFee = 20; // Defining the insurance fee
    var BOOKINGFEE = 50; // Defining the Booking fee
    var sum = 0 ; // Setting the sum variable to 0
    var cbResults = ' '; // Setting the extras to nothing so when there is something called, it won't save for the next booking
    outputEmail.innerHTML = emailInput.value; // Gathering the input of the email, storing it as a output for the booking summary
    outputComment.innerHTML = furtherCommentsInput.value; // Gathering the input of the further comments, storing it as a output for the booking summary
    outputDropOff.innerHTML = dropOffDate.value; // Gathering the input of the drop off date, storing it as a output for the booking summary
    for (var i = 0; i < form.elements.length; i++) { // For loop checking the elements
        if (form.elements[i].type == 'radio') { // If statement for the chosen vehicle (because its done in radios)
            if (form.elements[i].checked == true) { // If the radio is checked...
                vehicleResult = form.elements[i].value; // Gathering the chosen vehicle and storing it as "vehicleResult"
                vehicleCost = form.elements[i].dataset.price; // Gathering the cost of the chosen vehicle, storing it as "vehicleCost" for cost calculations 
                insuranceCost = Math.round(insuranceFee + vehicleCost * amountOfDays); // Calculating the cost of the vehicle, amount of days and insurance
                outputDays.innerHTML = amountOfDays; // Gathering the input of the amount of days, storing it as a output for the booking summary
                outputVehicle.innerHTML = vehicleResult; // Gathering the input of the chosen vehicle, storing it as a output for the booking summary
            }
        }
        if (form.elements[i].type == "checkbox") { // If statement for the chosen extras (because its done in checkboxes)
            if (form.elements[i].checked == true) { // If the checkbox is checked...
                cbResults += form.elements[i].value + ', '; // Checking how many extras the user has chosen
                sum = sum + parseInt(form.elements[i].dataset.price); // Calculating the cost of all the chosen extras 
                alert(cbResults + "$" + sum); // Sends a test alert to show the chosen extras
                outputExtras.innerHTML = cbResults; // Gathering the input of the chosen extras, storing it for the booking summary
                totalCost = Math.round(insuranceCost + sum + BOOKINGFEE); // Calculating the total cost
                outputCost.innerHTML = '$' + totalCost; // Gathering the input of the total cost, storing it for the booking summary
            }
        }
    }
}

I expect the total cost to be worked out and displayed in the booking summary.

p.s - ignore the code comments I need them there to pass




Aucun commentaire:

Enregistrer un commentaire