2017-03-05 40 views
-1

我收到了一個有趣的JavaScript練習練習,我試圖理解。下面的問題(截圖)需要用while循環來解決。彈跳球:彈跳次數和跑道距離

滾珠歸還的係數,0和1之間的數,指定當球擊中剛性表面多少能量守恆。例如,0.9的係數意味着彈跳球在每次彈跳後將上升到其先前高度的90%。

編寫一個程序,以米爲單位輸入恢復係數和初始高度,並報告球在從初始高度跌落到10釐米以下之前從最初高度跌落時彈起多少次。同時報告此點之前球的總距離。

網球,籃球,超級球和壘球的恢復係數分別爲.7,.75,.9和.3。

我想使用下面的代碼來完成這個,但它只是掛起。

function code1() { 
    var heightM = getInputOne(); 
    var heightCm = heightM * 100; 
    var coefficient = getInputTwo(); 
    var distance = getInputOne(); 
    var bounce = 0; 

    while (heightCm >= 10) { 
    bounce = bounce + 1; 
    distance = distance + (heightM * coefficient * 2); 
    heightCm = heightCm * coefficient; 
    } 
    console.log(bounce); 
    console.log(distance); 

} 

下面是被調用的函數內它

// Return the text in the 'In 1:' box 
function getInputOne() { 
    var input = getElement("inOne"); 
    return input.value; 
} 

// Return the text in the 'In 2:' box 
function getInputTwo() { 
    var input = getElement("inTwo"); 
    return input.value; 
} 

任何幫助,將不勝感激。另外,讓我知道其他數據可能有用。

回答

1

有幾個問題:

  • input.value屬性讀取的值是一個字符串,而不是數字。這影響+運營商在下面的表達式是如何工作的:

    distance = distance + (heightM * coefficient * 2); 
    

    由於distance是一個字符串,該+是串聯,而不是加法。所以distance只會增加一串數字。

  • 儘管您在每次迭代中都修改了heightCm,但您不要對heightM執行相同操作。它保持其原始值,所以距離計算是錯誤的。

  • 您應該檢查coefficient的輸入值是否在限制內。如果您允許值爲1或更大,則計算出的高度將在循環的每次迭代中增加,這會使其掛起。

所以我建議這樣的代碼:

function getBounces(heightM, coefficient) { 
 
    var distance = heightM; // avoid reading input twice 
 
    var bounce = 0; 
 

 
    while (heightM >= 0.10) { // just compare with meters... 
 
    bounce = bounce + 1; 
 
    heightM = heightM * coefficient; 
 
    distance = distance + heightM * 2; 
 
    if (bounce > 100) throw "error"; 
 
    } 
 
    return [bounce, distance]; 
 
} 
 

 
document.getElementById('calc').addEventListener('click', function() { 
 
    var heightM = getInputOne(); 
 
    var coefficient = getInputTwo(); 
 
    if (coefficient >= 1 || coefficient < 0) { 
 
    alert('invalid input for coefficient'); 
 
    return; 
 
    } 
 
    var result = getBounces(heightM, coefficient); 
 
    outputResults(result[0], result[1]); 
 
}); 
 

 

 
// Return the text in the 'In 1:' box 
 
function getInputOne() { 
 
    var input = document.getElementById("inOne"); 
 
    return +input.value; // add the + for conversion to number 
 
} 
 

 
// Return the text in the 'In 2:' box 
 
function getInputTwo() { 
 
    var input = document.getElementById("inTwo"); 
 
    return +input.value; // add the + for conversion to number 
 
} 
 

 
function outputResults(bounce, distance) { 
 
    var output = document.getElementById("bounce"); 
 
    output.textContent = bounce; 
 
    output = document.getElementById("dist"); 
 
    output.textContent = distance.toFixed(2); 
 
}
Height: <input id="inOne">m<br> 
 
Coefficient: <input id="inTwo"> (0 ... 0.99)<br> 
 
<button id="calc">Calculate</button> 
 
<br> 
 
Bounces: <span id="bounce"></span><br> 
 
Distance: <span id="dist"></span>m<br>

+0

非常感謝你,這有助於。我認爲主要問題在於字符串和整數問題。另外,我用高度替換了heightM和heightCM。最後,輸入錯誤檢查是一個好主意,一般我會在添加核心功能後添加類似的內容。 – Wyldbrian