I'm pretty new to webdesign and also to Javascript. While setting up my first web application I run into a bug that I don't understand. It must be something in the way I think Javascript works in webpages or something that I don't know about loops in general.
I'm trying to highlight the best value of a table using js. As long as the file is running locally on my PC, I generate random values and keep pressing the refresh button until a bug occurs. For debugging purposes I also use the console.log function now, but I still don't understand way this is happening...
Many thanks to any hint about what could be wrong in this actually pretty simple code.
// Best value of the week
var table = document.getElementsByTagName('table')[0];
var bestValue = 0;
var bestRow = 0;
for (var i = 1; i < 22; i++) {
if (table.rows[i].childNodes[5].innerHTML > bestValue){
bestValue = table.rows[i].childNodes[5].innerHTML;
console.log(i);
console.log(table.rows[i].childNodes[5].innerHTML);
console.log(bestValue);
bestRow = i;
}
else{
console.log(i);
console.log(table.rows[i].childNodes[5].innerHTML);
console.log(bestValue);
}
}
table.rows[bestRow].childNodes[5].classList.add('bestofweek');
