Reputation: 29
I have received an interesting javascript practice exercise that I'm trying to understand. The problem below (screenshot) needs to be solved with a while loop.
The coefficient of restitution of a ball, a number between 0 and 1, specifies how much energy is conserved when a ball hits a rigid surface. A coefficient of .9, for instance, means a bouncing ball will rise to 90% of its previous height after each bounce.
Write a program to input a coefficient of restitution and an initial hight in meters, and report how many times a ball bounces when dropped from its initial height before it rises to a height of less than 10 centimeters. Also report the total distance traveled by the ball before this point.
The coefficients of restitution for a tennis ball, basketball, super ball, and softball are .7, .75, .9, and .3, respectively.
I'm trying to use the code below to complete this, but it simply hangs.
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);
}
Here are the functions being called within it
// 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;
}
Any help with this would be appreciated. Also, let me know what other data might be useful.
Upvotes: 0
Views: 2074
Reputation: 350147
There are a few issues:
The value read from the input.value
property is a string, not a number. This effects how the +
operator works in the following expression:
distance = distance + (heightM * coefficient * 2);
As distance
is a string, the +
is a concatenation, not an addition. And so distance
will just be growing string of digits.
although you adapt heightCm
in every iteration, you don't do the same with heightM
. It maintains its original value, and so the distance calculation is wrong.
You should check that the input value for coefficient
is within the limits. If you allow a value of 1 or more, then the calculated height will increase in every iteration of the loop, which will make it hang.
So I would suggest this code:
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>
Upvotes: 2