Good day, I'm coding a simple calculation price based on: A single price The frequency of that price (Weekly, daily, etc.).
I keep having the word "undefined" being printed out in the page. I've tried to redefine all the variable of that part. All variables are correctly defined. Do you guys see any error in my code below?
//parking cost
var parking_cost1 = 0;
var parkingCostUnity1 = 0;
var parkingCostFrequency1 = 0;
parking_cost1 = new Number(document.getElementById("parking_cost1").value);
parkingCostFrequency1 = document.getElementById("parking_cost1_frequence1").value;
if (parkingCostFrequency1 == "per_night") {
parkingCostUnity1 = result;
}
if (parkingCostFrequency1 == "per_week") {
parkingCostUnity1 = nrOfWeeks;
}
if (parkingCostFrequency1 == "per_month") {
parkingCostUnity1 = nrOfMonths;
}
var echoParkingCostFrequency1 = parkingCostFrequency1.replace("_", " ");
var totalParkingCost1 = 0;
totalParkingCost1 = parkingCostUnity1 * parking_cost1;
if (totalParkingCost1 > 0) {
var printOutTotalParkingCost1 = '<tr><td><b>Total parking cost</b></td><td>' + parking_cost1 + ' ' + currency + ' ' + echoParkingCostFrequency1 + '</td><td>' + totalParkingCost1 + ' ' + currency + '</td></tr>';
}
<i class="fas fa-parking"></i> <text class="form_basic_text" />Parking cost<br>
<input type="number" onChange="resume()" name="parking_cost1" id="parking_cost1" class="form_basic_field_bid" style="width:auto;" /><br/>
<center>
<select onChange="resume()" name="parking_cost1_frequence1" id="parking_cost1_frequence1" style="width:auto;height:25px;padding:0px;width:auto;">
<option value="per_night">Per night</option>
<option value="per_week">Per week</option>
<option value="per_month">Per month</option>
</select>
</center>
The following variables have already been defined in the document:
//var currency
var currency = document.getElementById("currency").value;
//This variable is always defined, as it's a dropdown.
//var result, nrOfWeeks and nrOfMonths are all under the same parameters
//check-in date
var checkInDate = new Date(document.getElementById("check_in_date").value);
var checkInDateResume = checkInDate.getDate() + '-' + (checkInDate.getMonth()+1) + '-' + checkInDate.getFullYear();
if(checkInDateResume=="NaN-NaN-NaN"){
checkInDateResume = "<text class='alert'>Not defined yet</text>";
result = 1;
nrOfWeeks = 1;
nrBiWeeks = 1;
nrOfMonths = 1;
}
var checkOutDate = new Date(document.getElementById("check_out_date").value);
var checkOutDateResume = checkOutDate.getDate() + '-' + (checkOutDate.getMonth()+1) + '-' + checkOutDate.getFullYear();
if(checkOutDateResume=="NaN-NaN-NaN"){
checkOutDateResume = "<text class='alert'>Not defined yet</text>";
result = 1;
nrOfWeeks = 1;
nrBiWeeks = 1;
nrOfMonths = 1;
}
//Since the nr of months, week and result (the number of nights) is the difference between the check-in and check-out date - Those aren't defined except as mentionned up here. We then define the rest of the vars
var difference = Math.abs(checkOutDate-checkInDate);
var result = difference/(1000*3600*24);
var nrOfMonths = Math.ceil(result/30.41);
var nrOfWeeks = Math.ceil(result/7);
Thank you!
Edit: It's my first time posting here. Thank you for your patience and your comments on how to make my question more understandable!
I did some extended research and discovered my mistake: I forgot to add an else to the the last IF statement.
if(totalParkingCost1>0){
var printOutTotalParkingCost1 = 'XYZ';
}
The var printOutTotalParkingCost1 was used in any scenario. I added the following else
else{
var printOutTotalParkingCost1 = "";
}
Now, the variable printOutTotalParkingCost1 is defined whatever the scenario is. Thank you all for your kind help! Thanks to @ivar for his extensive explanations.