我想添加用户输入,但是出了点问题。没有错误,但没有任何反应。我要确保输入为数字,然后将两个输入加在一起。
override func setSelected(_ selected: Bool, animated: Bool) {
let color = stripeView.backgroundColor
super.setSelected(selected, animated: animated)
if selected {
stripeView.backgroundColor = color
}
}
override func setHighlighted(_ highlighted: Bool, animated: Bool) {
let color = stripeView.backgroundColor
super.setHighlighted(highlighted, animated: animated)
if highlighted {
stripeView.backgroundColor = color
}
}
function add() {
var first = document.getElementById('first-number').value;
var second = document.getElementById('second-number').value;
firstMostBe = first;
secondMostBe = second;
var output;
if ((isNaN(firstMostBe)) || (isNaN(secondMostBe))){
alert('one or both of the operands are not numbers');
} else {
output = firstMostBe + secondMostBe;
return output;
}
var result = output; // place-holder, delete this and start over
var output_field = document.getElementById('result');
output_field.innerHTML = result;
}
答案 0 :(得分:0)
您不需要dots: [
...((previousValue[date] && previousValue[date].dots) || []),
{
key:currentValue._id,
color: 'black'
}
]
的函数,也不需要使用Unary Plus return
将输入转换为数字。您可以通过以下方式使代码更简洁
+
和['first-number','second-number']
,然后对其进行解构。
map()
function add() {
var [first,second] = ['first-number','second-number'].map(x => +document.getElementById(x).value)
if ([first,second].some(x => isNaN(x))){
alert('one or both of the operands are not numbers');
} else {
document.getElementById('result').innerHTML = first * second
}
}
<h2>Three-Layer Handlers Exercises</h2>
<hr>
<script src="./handlers-calculator.js"></script>
first number: <input id='first-number' value=''></input><br>
second number: <input id='second-number' value=''></input> <br>
<button onclick='add()'>+</button>
<button onclick='subtract()'>-</button>
<button onclick='multiply()'>*</button>
<button onclick='divide()'>/</button> <br>
<p id='result'></p>
<br><br>
<hr>
<hr>
返回包含两个输入的两个值的数组。然后解构。等效于:
['first-number','second-number'].map(x => +document.getElementById(x).value)
答案 1 :(得分:0)
您需要操作数的数值(带有unary plus +
),如果您有NaN
值,则需要提早返回。
稍后,您需要将运算结果分配给目标元素。
之间,您无需返回,因为这样可以阻止运行直到分配结果的位置。
function add() {
var first = +document.getElementById('first-number').value, // try to get a number
second = +document.getElementById('second-number').value, // try to get a number
output_field = document.getElementById('result');
if (isNaN(first) || isNaN(second)) {
alert('one or both of the operands are not numbers');
return; // exit
}
output_field.innerHTML = first + second;
}
<h2>Three-Layer Handlers Exercises</h2>
first number: <input id='first-number' value=''><br>
second number: <input id='second-number' value=''><br>
<button onclick='add()'>+</button>
<button onclick='subtract()'>-</button>
<button onclick='multiply()'>*</button>
<button onclick='divide()'>/</button> <br>
<p id='result'></p>
答案 2 :(得分:0)
function add() {
var first = document.getElementById('first-number').value;
var second = document.getElementById('second-number').value;
firstMostBe = first;
secondMostBe = second;
var output;
if (isNaN(firstMostBe) || isNaN(secondMostBe)) {
alert('one or both of the operands are not numbers');
} else {
output = parseInt(firstMostBe) + parseInt(secondMostBe);// By defaut it is string need to convert to number
// no need to return form here
}
var result = output; // place-holder, delete this and start over
var output_field = document.getElementById('result');
output_field.innerHTML = result;
}
有2个问题