I'm looking for a code review of some javascript I wrote - this code very simply counts in the direction of a particular target number, with the count slowing down as the target gets nearer.
I'm looking for (and be as brutal as you like) ways to improve the code or the algorithm (I'm aware there should be comments) - I'm a recreational programmer and would like to be improving my skills. My next step is to get the targetNumber
value from a nearby file without losing the current count - If anyone wants to point out an elegant way of doing this feel free, I'm planning on having a good think on it over the weekend.
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN "http://www.w3.org/TR/html4/strict.dtd">
<html>
<head>
<title>Count to a particular target</title>
</head>
<body>
<h1 id="myDiv">Starting</h1>
<script type="text/javascript">
currentValue = 100;
targetValue = 1500;
function count() {
if (currentValue > targetValue) {
currentValue -= 1
} else if (currentValue < targetValue) {
currentValue += 1
}
document.getElementById('myDiv').innerHTML = 'Total wordcount:'+ currentValue.toString();
changeTime = 20;
if (Math.abs(currentValue - targetValue) < 980) {
changeTime = 1000 - Math.abs(currentValue - targetValue);
}
setTimeout(count,changeTime/2);
}
count()
</script>
</body>
</html>