html 我如何获得偶数/奇数检查以打印结果?

kqqjbcuj  于 2023-08-01  发布在  其他
关注(0)|答案(3)|浏览(158)

我有东西要打印,但它不是正确的答案。现在我只是得到"[object HTMLFormElement] is a Odd number",不管我输入什么数字,它是偶数还是奇数。

function isEvenorOdd(num, form) {
    if (num % 2 == 0) {
        document.getElementById('result').innerHTML = `${num} is a Even number`
        console.log(`${num} is a Even number`)
    }
    else {
        document.getElementById('result').innerHTML = `${num} is a Odd number`
        console.log(`${num} is a Odd number`)
    }
}

个字符

6rqinv9w

6rqinv9w1#

你的代码有几个问题

  • isEvenorOdd函数-您有两个参数numform。但是当你在onclick属性中调用函数时,你只传递了表单元素(this.form),而不是输入字段。
  • 实际上并不是从输入字段中获取值
  • 您的输入字段没有JS无法确定或获取值的ID。
    这里有一个工作示例-相当简单:
function isEvenorOdd() {
        let num = document.getElementById('num').value;

        // Check if number is valid
        if (isNaN(num)) {
            document.getElementById('result').innerHTML = `Please enter a valid number`;
            return;
        }

        num = parseInt(num);

        if (num % 2 == 0) {
            document.getElementById('result').innerHTML = `${num} is an Even number`
            console.log(`${num} is an Even number`)
        }
        else {
            document.getElementById('result').innerHTML = `${num} is an Odd number`
            console.log(`${num} is an Odd number`)
        }
    }

个字符

大整型示例:用于用例1111111111111111111111111111111111112

function isEvenorOdd() {
        let num = document.getElementById('num').value;

        // Check if number is valid
        if (isNaN(num)) {
            document.getElementById('result').innerHTML = `Please enter a valid number`;
            return;
        }

        num = BigInt(num);

        if (num % 2n == 0n) {
            document.getElementById('result').innerHTML = `${num} is an Even number`
            console.log(`${num} is an Even number`)
        }
        else {
            document.getElementById('result').innerHTML = `${num} is an Odd number`
            console.log(`${num} is an Odd number`)
        }
    }
Check if numbers are even or odd:
    <form>
        <input type="text" id="num">
        <input type="button" value="Check" onclick="isEvenorOdd();">
    </form>
    <div id="result"></div>

的字符串

2wnc66cl

2wnc66cl2#

如前所述,您传递的是表单对象,而不是字段的数值。你需要得到文本输入,得到它的值,解析为int,然后调用isEvenorOdd

function isEvenorOdd(num, form) {
    if (num % 2 == 0) {
        document.getElementById('result').innerHTML = `${num} is a Even number`
        console.log(`${num} is a Even number`)
    }
    else {
        document.getElementById('result').innerHTML = `${num} is a Odd number`
        console.log(`${num} is a Odd number`)
    }
}

个字符

tjjdgumg

tjjdgumg3#

与其在HTML上使用onclick函数,不如像这样使用eventListeners

const button = document.querySelector('input[type=button]');;//get the button element
const result = document.getElementById('result')
//addEventListener to the button
button.addEventListener("click", function(){
    let num = document.querySelector('input[type=text]').value;//get the input text only on each button press to ge tthe value every time
   //Number() to convert it to a number then compare, also validate if number is NaN
  if(!isNaN(num)){
    num = Number(num);
    if (num % 2 == 0) {
      result.textContent = `${num} is an Even number`;
    } else {
      result.textContent = `${num} is an Odd number`;
    }
  } else {
    result.textContent = `your input "${num}" is not a number, plz try again`;
  }
})

个字符
另外,如果您只打算更改元素的文本,那么就性能而言,使用textContentinnerHTML要好得多。
你还必须验证用户是否真的输入了数字,因为这是一个输入文本,他们可以输入任何他们想要的,而不一定是一个数字。如果他们输入像Foo这样的东西,那么当你用Number()将其转换为数字时,你必须用isNaN()进行验证,如果这返回NaN,那么这意味着他没有输入数字,因此它不能用Number()进行转换,你可以返回像${num} is not a number这样的东西

相关问题