gpt4 book ai didi

I've been making a midpoint calculator but the results are all wrong. any help will be useful(我一直在做一个中间点计算器,但结果都错了。任何帮助都将是有用的)

转载 作者:bug小助手 更新时间:2023-10-24 20:53:33 26 4
gpt4 key购买 nike



<!DOCTYPE html>
<html>
<input type="text" placeholder ="first number" id="first">
<br><br><br>
<input type="text" placeholder="second number" id="second">
<br><br><br>
<button type="submit" onclick="myfunc()">Calcuate</button>

<script type="text/javascript">
function myfunc() {
var first = document.getElementById('first').value;
var second = document.getElementById('second').value;
alert((first+second)/2);
}
</script>
</html>

the results usually end up either correct or extremely off, like sometimes when I try to calculate the midpoint of 1 and 10 it shows up as 55

结果通常要么是正确的,要么是非常不正确的,就像有时我试图计算1和10的中点时,结果显示为55


更多回答

Have you tried parsing your input as a number with parseInt() or something similiar? If there are any extra spaces or if the input is not properly read as an actual number, your results will be incorrect.

您是否尝试过使用parseInt()或类似的方法将输入解析为数字?如果有任何额外的空格,或者如果输入没有正确读取为实际数字,您的结果将不正确。

优秀答案推荐

The value property is a string, so + performs string concatenation rather than numeric addition. Instead, you could use inputs with type set to "number" and read the valueAsNumber property.

Value属性是一个字符串,因此+执行字符串连接而不是数字相加。相反,您可以使用类型设置为“Number”的输入,并读取valueAsNumber属性。




document.querySelector('button').addEventListener('click', e => {
const mid = (document.getElementById('first').valueAsNumber
+ document.getElementById('second').valueAsNumber) / 2;
console.log(mid);
});

input[type=number] {
display: block;
margin-bottom: 37px;
}

<input type="number" placeholder="first number" id="first">
<input type="number" placeholder="second number" id="second">
<button>Calculate</button>





.value returns results as text. So when you enter 1 for the first input field and 10 for the second input field, and then use +, you are doing string concatenation ("1" + "10" = 110) rather than numeric addition. You may find the parseInt method to be of help.

.Value以文本形式返回结果。因此,当您为第一个输入字段输入1,为第二个输入字段输入10,然后使用+时,您进行的是字符串连接(“1”+“10”=110),而不是数字相加。您可能会发现parseInt方法很有帮助。


As a broader note, you have both of those input fields specified as text, which means I could enter "one" and "ten" and that would be accepted, but throw an error when doing mathematics. You might want to consider if there's another input type that would restrict entry to numbers or explicitly handle that case in your code.

作为更广泛的说明,您将这两个输入字段都指定为文本,这意味着我可以输入“1”和“10”,这将被接受,但在计算数学时会抛出一个错误。您可能需要考虑是否有另一种输入类型将输入限制为数字或在代码中显式处理这种情况。



Try to parse each input first as an integer. This can help you avoid the issue where the result is incorrectly calculated if the "number" provided in the <input> is actually a string instead.

尝试首先将每个输入解析为一个整数。这可以帮助您避免在中提供的“number”实际上是一个字符串时计算结果不正确的问题。


<!DOCTYPE html>
<html>
<input type="text" placeholder ="first number" id="first">
<br><br><br>
<input type="text" placeholder="second number" id="second">
<br><br><br>
<button type="submit" onclick="myfunc()">Calcuate</button>

<script type="text/javascript">
function myfunc() {

//Parse the value, which may be a string, as an integer. If the input is not a valid integer, an error will be thrown accordingly
var first = parseInt(document.getElementById('first').value);
var second = parseInt(document.getElementById('second').value);
alert((first+second)/2);
}
</script>
</html>

On a further note, using parseInt() will parse numbers with decimals as integers. You may need to take this into account when doing your calculations where the input values may be a float.

另外,使用parseInt()将把带有小数的数字解析为整数。在进行输入值可能为浮点数的计算时,可能需要考虑这一点。



js took the text in the textbox as a string you need to convert to variables to integers then print the answer here

JS将文本框中的文本作为字符串,您需要将变量转换为整数,然后在此处打印答案


code:

代码:


<!DOCTYPE html>
<html>
<input type="text" placeholder ="first number" id="first">
<br><br><br>
<input type="text" placeholder="second number" id="second">
<br><br><br>
<button type="submit" onclick="myfunc()">Calcuate</button>

<script type="text/javascript">
function myfunc() {
var first_as_string = document.getElementById('first').value;
var second_as_string = document.getElementById('second').value;
const first = parseInt(first_as_string);
const second = parseInt(second_as_string);
alert((first+second)/2);
}
</script>
</html>```



The problem with your current is that "first" and "second" variables are retrieved as string so the Javascript engine will do a concatenation then convert it to number and make division operation.

当前的问题是“First”和“Second”变量是以字符串的形式检索的,所以Java引擎将进行连接,然后将其转换为数字并进行除法运算。


Ex: first = "1", second = "2", first+second = "12" and then (first+second)/2 = 6

例如:First=“1”,Second=“2”,First+Second=“12”,然后(First+Second)/2=6


So you have to convert the variables into numbers beforehand.

所以你必须事先把变量转换成数字。


Your solution might be:

您的解决方案可能是:


<!DOCTYPE html>
<html>
<input type="text" placeholder ="first number" id="first">
<br><br><br>
<input type="text" placeholder="second number" id="second">
<br><br><br>
<button type="submit" onclick="myfunc()">Calcuate</button>

<script type="text/javascript">
function myfunc() {
var first = document.getElementById('first').value;
var second = document.getElementById('second').value;
alert((Number(first)+Number(second))/2);
}
</script>


更多回答

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com