简体   繁体   English

“未定义”错误在 Javascript 中打印出来

[英]"undefined" error prints out in Javascript

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 语句中添加 else。

if(totalParkingCost1>0){
var printOutTotalParkingCost1 = 'XYZ';
}

The var printOutTotalParkingCost1 was used in any scenario. var printOutTotalParkingCost1 用于任何场景。 I added the following else我添加了以下其他内容

else{
var printOutTotalParkingCost1 = "";
}

Now, the variable printOutTotalParkingCost1 is defined whatever the scenario is.现在,无论场景如何,都定义了变量 printOutTotalParkingCost1。 Thank you all for your kind help!谢谢大家的热心帮助! Thanks to @ivar for his extensive explanations.感谢@ivar 的详尽解释。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 JavaScript 打印层层叠叠的星星,为什么for循环最后出现undefined? - Why appear undefined in the end of for-loop in JavaScript when I try to prints out layers of stars? Javascript错误:内存不足或&#39;Telerik&#39;未定义 - Javascript error: Out of memory or 'Telerik' is undefined Javascript,console.log打印prints对象,但属性未定义 - Javascript, console.log prints prints object, but property is undefined Javascript,无法读取未定义的属性,但打印到控制台 - Javascript, Cannot read property of undefined, but prints to console Javascript生成的HTML还会打印“ undefined” - Javascript-generated HTML also prints “undefined” 将javascript变量发送到php并打印出“Array” - sent javascript variable to php and it prints out “Array” 排列算法打印出部分错误消息 - Permutations algorithm prints out parts of error message 控制台打印出未定义的内容。 我的代码有什么问题? - Console prints out undefined. What is wrong with my code? 以div输出,以数组形式打印对象,但第一个答案是“未定义”。 -Javascript - Output in div, prints objects in array, but first answer is “Undefined”. - Javascript 当在JavaScript中读取节点时,Firebase Cloud Function打印“未定义”吗? - Firebase Cloud Function prints 'undefined' when reading a node in JavaScript?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM