[英]Number.isInteger(this) doesn't work in Number.prototype method
[英]I need to check if a number is not a decimal, I'm trying with Number.isInteger() but doesn't work
我想要一個提示警報不斷彈出,直到用戶輸入一個沒有小數的數字,並且由於某種原因,我不明白 Number.isInteger 不起作用,例如它接受 4.4 或 8.333 ...是那個數字嗎? isInteger 用小數計算為真?
檢查這個片段:
function till() { let x = prompt("ingrese numero") if (Number.isInteger(parseInt(x)) === false) { till() } } till() console.log("FInally out!")
您的問題是parseInt
function 只是將 x 轉換為Integer
因此即使您輸入 8.333 它也被視為 8。
這是解決您問題的簡單方法!
function till() { let x = prompt("ingrese numero") if (parseInt(x).= parseFloat(x)) { till() } } till() console.log("FInally out!")
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.