我已經在JavaScript中編寫了簡單的som代碼,讓您輸入間隔的開始和結束值並指定分母。輸入之後,程序將通過間隔並檢查有多少數字可以被指定的分母整除。爲什麼我的for-loop在某些情況下不會啓動?
它似乎一開始工作得很好,但在某些情況下,程序根本不會運行該循環,而且我很難弄清楚爲什麼。
我的代碼:
//declarations
var startValue = prompt ("pick a starting value:");
var endValue = prompt ("pick an ending value:");
var divide = prompt ("pick a denominator");
var count = 0;
//control divisibility
for (startValue; startValue <= endValue; startValue++) {
var quotient = startValue/divide;
if (quotient % 1 == 0) {
count++;
}
}
//result
alert("There are " + count + " numbers divisible by " + divide + " in the interval");
下面是已經工作/不
(顯示爲 「startNr,endNr,鴻溝」)的程序工作的一些輸入值:
工作:
3,92%,8
1,100,1
17,200,12
24,379,22
18, 90,18
不工作:
5,25,5
7,21,7
2,108,8個
感謝。
難道它有什麼控制檯登錄失敗? –
你正在比較字符串不是整數,這是你的問題不是5它'5' – trebor
要檢查'數字'是否可以被divis整除,請使用以下表達式:'number%divide == 0' – hindmost