- “Your calcWinner takes almost double the time than the other one.” Not hundres of times.
- I changed the restart function this way for (var square of squares) square.value = Math.random() >=0.33 ? (Math.random() >=0.33 ? ‘X’ : ‘O’) : ‘’; and console times don’t change much.
- Yes, yours takes almost double the time of the other (no need of performace.now()).
- Doesn’t matter for my points. I falsified your assertion telling that you check code (calcWinner) was better.
I dislike your code because:
- there is no separation of concerns: there is no distinction between model, view and controller. As if you do not what a MVC concept is.
- Functions are not in “single responsibility”: from a calcWinner() function I expect to return the winner, if there is one. But yours change the turn (nextPlayer()) or updates the view (txtTurn.textContent = “Winner”;). If you want to add an AI, for example, calcWinner needs to be rewritten in order to not modify anything of the view while processing the tree of matches.
- The choice of using inputs as part of the model ties the JS logic to the view. What if we want to use images instead of inputs?
- Formatting is terrible, it seems as if you waste time formatting things instead of using a formatter. But may be I am wrong.
- Modifying a value inside an if clause (if (++turn ==…), is prone to errors and really a bad practice. Your code is full of side effects. It is as if you never worked with others.
I have the strong suspect that you don’t know how web development works. Do you use webpack or similar bundlers? I am asking because in some other articles you said that JS modules are useless…
About let and cost, it is clear what they do and should be used instead of var, which is almost never useful. const it is not used to prevent an object to be modified or to tell the reader that object is unmodifiable. It is used to tell the VM and the reader that variabile will not be reassigned, allowing optimizations VM side, for example. It is not the only difference, but that is not the point. The point is that JS language evolved, you didn’t.