Quote:
Originally Posted by Philip M
While many people are still using older browsers, it is pointless to code in such a way that it works only in browsers which support HTML5. In a few year's time - perhaps.
|
That comment is completely irrelevant to this particular situation as there is nothing that has been discussed here that is limited to browsers that support HTML 5. In any case support or otherwise for HTML 5 has nothing to do with JavaScript.
In the first post I made where I referenced filter() as the obvious solution to this particular problem I also included a link to a page that displays the code needed to implement it for ALL browsers that support JavaScript. So in browsers that support modern JavaScript the call would use the native filter method built into the browser and for browsers that only support antiquated JavaScript the additional code to define the filter method within the script itself would run.
Any of the posted solutions using filter will therefore work in all browsers currently in use and not just those that support the latest version of JavaScript.
Also by viewing the code needed to implement filter in browsers still using antiquated versions of JavaScript anyone can see how to implement a loop to iterate over an array so as to perform processing on each element. Even a beginner would be able to identify how to define a loop to iterate over an array from that code - which is half of the solution they need.
Also while HTML 5 is still at an early draft stage, the modern JavaScript version that includes the filter method became the official JavaScript standard back in 2011 and so filter is a standard JavaScript method.
Anyway the wording of this particular question is such that it probably originated as one intended to find out whether the person recognised that the filter method is the solution to such problems - what the problem asks you to do matches so closely to what the filter function is designed to do that it makes no sense to use the problem for anything else other than to test if the person knows about filter. Using the problem that way would make any answer that doesn't use filter wrong.
I mostly agree with you about execution speed. Readability of the code so as to make it easier to maintain is far more important and will save far more time than using more convoluted code that results in a minor speed increase. Where there are two alternatives for coding that are equally readable and where one is faster than the other then it makes sense to choose the faster alternative as the way you write your code. Where the faster version also results in shorter and more readable code then that becomes the obvious choice.