我正在写一个小的“报价计算器”。我想使用innerHTML
将摘要打印到浏览器中,但是我仍然得到“未定义” 而不是价格。但是在我的console.log
一切正常的情况下,我可以console.log
可变价格并获得预期的结果。
//Variables
const form = document.getElementById('request-quote');
const html = new HTMLUI();
//Event Listeners
eventListeners();
function eventListeners() {
document.addEventListener('DOMContentLoaded', function() {
//Create the <option> for the years
html.displayYears();
});
//when the form is submitted
form.addEventListener('submit', function(e) {
e.preventDefault();
//get values from the form
const make = document.getElementById('make').value;
const year = document.getElementById('year').value;
//Read the radio buttons
const level =
document.querySelector('input[name="level"]:checked').value;
// Validation
if (make === '' || year === '' || level === '') {
html.displayErrors('All fields are mendatory')
} else {
//Make the quotation
const insurance = new Insurance(make, year, level);
const price = insurance.calculateQuotation(insurance);
//print
html.showResults(price);
}
});
}
// Create another prototype with for the object HTMLUI. to print Errors
//Object
// Everything related to the calculation and quotation
function Insurance(make, year, level) {
this.make = make;
this.year = year;
this.level = level;
}
//calculation
Insurance.prototype.calculateQuotation = function(insurance) {
let price;
const base = 2000;
//get make
const make = insurance.make;
/**
1. america: 15
2. Asia : 5
3. europia: 35
*/
switch (make) {
case '1':
price = base * 1.15;
break
case '2':
price = base * 1.05;
break
case '3':
price = base * 1.35;
break
}
//get level
const level = insurance.level;
price = this.calculateLevel(price, level);
//get year
const year = insurance.year;
const difference = this.getYearDifference(year);
price = price - ((difference * 3) * price) / 100;
console.log(price);
}
//return difference between years
Insurance.prototype.getYearDifference = function(year) {
return new Date().getFullYear() - year;
// each year the cost must be 3% cheaper
}
//add value based on level
Insurance.prototype.calculateLevel = function(price, level) {
//basic increase 30%
//complete increases 50%
if (level === 'basic') {
price = price * 1.30;
} else {
price = price * 1.50;
}
return price;
}
function HTMLUI() {}
//display the latest 20 years in the select
HTMLUI.prototype.displayYears = function() {
//Max & minimum years
const max = new Date().getFullYear();
min = max - 20;
//Generate the list
const selectYears = document.getElementById('year');
//print the values
for (let i = max; i >= min; i--) {
const option = document.createElement('option')
option.value = i;
option.textContent = i;
selectYears.appendChild(option);
}
}
//Print Error, by creating a prototype
HTMLUI.prototype.displayErrors = function(message) {
//create div
const div = document.createElement('div');
div.classList = 'error';
//insert message
div.innerText = `
<p>${message}</p>
`;
form.insertBefore(div, document.querySelector('.form-group'));
//Remove the error
setTimeout(function() {
document.querySelector('.error').remove();
}, 3000);
}
HTMLUI.prototype.showResults = function(price) {
//print result
const result = document.getElementById('result');
//create a div with the result
const div = document.createElement('div');
//insert the result
div.innerHTML = `
<p class="total">Total: $ ${price}</p>
`;
//insert into html
result.appendChild(div)
}
我希望打印可变价格(实际上将是价格)的值,但是在尝试打印价格时出现“ undefined”
答案 0 :(得分:1)
您需要在def extract_movie_titles(name):
p = get_movies_from_tastedive(name)
return[d['Name'] for d in p['Similar']['Info']]
print(extract_movie_titles(get_movies_from_tastedive("Tony Bennett"))) #line 22
print(extract_movie_titles(get_movies_from_tastedive("Bridesmaids"))) #line 23
函数中添加return
语句。如果有帮助,这就是为什么。
获得calculateQuotation
的原因是,从未向undefined
变量赋值。
在这一行:
price
const price = insurance.calculateQuotation(insurance);
获取price
函数通过calculateQuotation
语句发送回的任何内容。但是,在该函数中,您只是填充仅在该函数的上下文中存在的return
变量。为了使这项工作有效,我相信您需要在price
函数的末尾添加一个return
语句,如下所示:
calculateQuotation
答案 1 :(得分:1)
显示未定义原因,您从未返回结果。您需要返回“ price”变量以对其进行修复。
cv2.threshold(img, 128, 255,cv2.THRESH_BINARY_INV | cv2.THRESH_OTSU)
cv2.fastNlMeansDenoising(img,None,60,10,20)