2017-07-03 66 views
1

做練習,我想不通爲什麼我的while循環不逃避,它口口聲聲說的值是無效的,即使是隻輸入數字。有任何想法嗎?爲什麼我的while循環沒有逃脫? (JavaScript)的

// 31. Karvonen Heart Rate 
 
var age, restingPulse, targetRate, numCheckAge, numCheckPulse, valid; 
 
    
 
    valid = false; 
 
    numCheckAge = /^\d+$/.test(age); 
 
    numCheckPulse = /^\d+$/.test(restingPulse); 
 
    
 
    while(valid === false) { 
 
     age = parseFloat(prompt("Enter your age:")); 
 
     restingPulse = parseFloat(prompt("Enter your resting heart rate:")); 
 
     if(numCheckAge === true && numCheckPulse === true) { 
 
     valid = true; 
 
     } else { 
 
     alert("Sorry. That's not a valid input, please enter numbers."); 
 
     } 
 
    } 
 
    
 
    for (var i = 0.55; i < 1; i+=0.05){ 
 
     targetRate = (((220 - age) - restingPulse) * i) + restingPulse; 
 
     document.write("Intensity: " + Math.round((i * 100)) + "% | Rate: " + Math.round(targetRate) + "<br>"); 
 
    }

+2

您需要執行的循環內的正則表達式的'test's(age'和'restingPulse'已進入後')。 – jcaron

+1

使用調試器會立即顯示'numCheckAge'和'numCheckPul​​se'設置只有一次,也不會更新以後... – jcaron

+0

另外,你可以只使用'isNaN(年齡)',而不是正則表達式來檢查,如果輸入是一個號碼 –

回答

1

我已經簡化和flexified你的代碼;)

檢查了這一點:

 
 
function requireNumericPrompt(text) { 
 
    var value; 
 
    while(true) { 
 
    if(/^\d+$/.test(value = prompt(text))) { 
 
     return value; 
 
    } 
 
    alert("Sorry. Please enter valid (numeric) value."); 
 
    } 
 
} 
 

 
var age = parseInt(requireNumericPrompt("Enter your age:")); 
 
var restingPulse = parseInt(requireNumericPrompt("Enter your resting heart rate:")); 
 

 
document.write('Age: ' + age + '<br/>'); 
 
document.write('Resting pulse: ' + restingPulse + '<br/>'); 
 
for (var targetRate, i = 0.55; i < 1; i+=0.05){ 
 
    targetRate = (((220 - age) - restingPulse) * i) + restingPulse; 
 
    document.write("Intensity: " + Math.round((i * 100)) + "% | Rate: " + Math.round(targetRate) + "<br>"); 
 
}

+1

完美,謝謝! – AKL012

+0

快樂是有幫助的(: – num8er

+1

注意,如果他們真正希望使用/ \ d + $ /不會匹配花車的持股量將被轉換爲字符串,該模式將不匹配小數 –

1

在哪里哪里,你測試正則表達式的點,這些變量agerestingPulseundefined。你必須在循環中進行測試的代碼移到,這些變量已收到來自用戶的輸入值:

while(!valid) { 
    age = parseFloat(prompt("Enter your age:")); 
    restingPulse = parseFloat(prompt("Enter your resting heart rate:")); 

    numCheckAge = /^\d+$/.test(age); 
    numCheckPulse = /^\d+$/.test(restingPulse); 
    if(numCheckAge && numCheckPulse) { 
    valid = true; 
    } 
    else { 
    alert("Sorry. That's not a valid input, please enter numbers."); 
    } 
} 

此外,您還可以刪除=== true位,使用的真值變量直接用於測試。

1

var age, restingPulse, targetRate, numCheckAge, numCheckPulse, valid; 
 

 
valid = false; 
 
numCheckAge =function(a) { 
 
\t return /^\d+$/.test(a); 
 
} 
 
numCheckPulse = function(a){ 
 
return /^\d+$/.test(restingPulse); 
 
} 
 

 
while(valid === false) { 
 
    age = parseFloat(prompt("Enter your age:")); 
 
    restingPulse = parseFloat(prompt("Enter your resting heart rate:")); 
 
    if(numCheckAge(age) && numCheckPulse(restingPulse)) { 
 
    valid = true; 
 
    } else { 
 
    alert("Sorry. That's not a valid input, please enter numbers."); 
 
    } 
 
} 
 

 
for (var i = 0.55; i < 1; i+=0.05){ 
 
    targetRate = (((220 - age) - restingPulse) * i) + restingPulse; 
 
    document.write("Intensity: " + Math.round((i * 100)) + "% | Rate: " + Math.round(targetRate) + "<br>"); 
 
}

你需要便利着想將你的檢查代碼改爲一個函數,而不是一個變量。

0

你在正確的軌道,你剛纔忘了初始化變量上。

var age, restingPulse, targetRate, numCheckAge, numCheckPulse, valid; 
 

 
age = 0; 
 
restingPulse = 0; 
 

 
valid = false; 
 
numCheckAge = /^\d+$/.test(age); 
 
numCheckPulse = /^\d+$/.test(restingPulse); 
 

 
while(valid !== true) { 
 
    age = parseFloat(prompt("Enter your age:")); 
 
    restingPulse = parseFloat(prompt("Enter your resting heart rate:")); 
 
    if(numCheckAge === true && numCheckPulse === true) { 
 
     valid = true; 
 
    } else { 
 
     alert("Sorry. That's not a valid input, please enter numbers."); 
 
    } 
 
} 
 

 
for (var i = 0.55; i < 1; i+=0.05){ 
 
    targetRate = (((220 - age) - restingPulse) * i) + restingPulse; 
 
    document.write("Intensity: " + Math.round((i * 100)) + "% | Rate: " + Math.round(targetRate) + "<br>"); 
 
}

+0

驗證0值爲什麼 – num8er

+0

事業,因爲你在模仿OOP使用JavaScript,除非該變量有靜態的。?存儲空間,它的初始值是不確定的 –

+0

OK,但它將如何檢查已進入數據之前if語句:'如果(numCheckAge ===真&& numCheckPul​​se ===真){'? – num8er