Manas Kumar is right. We simply don't need to consider your
switch
, wrong or not. Besides, hard-coding "5" might be bad: you are not reusing the constant and, hence, can have trouble supporting the code.
But there is one more problem, a deeper one. The condition
i === 5
suggests that you are not sure that
i
is numeric. But you are using the condition
i <= 5
above, which suggests that
i
should already be tested as numeric, which is not the case. You have to do the '===' check
before numerical comparison. And this is another reason for not considering your
switch
piece of code. Review of modified code could make sense only if your original code is correct, and if it's known what it achieves. Due to the problem I described, this is not the case. If this '===' condition makes sense at all,
switch
should not be used.
The identity (strict equality) operator '===', as opposed to equality operator '==', is explained, for example, here:
Comparison operators — JavaScript | MDN[
^].
The practice of using JavaScript by "absolute beginners" (as you said) is interesting and, well… questionable. This language is pleasure to use; being very simple and elegant, it is, conceptually, quite tricky and unusual, is the world's most misunderstood language:
JavaScript: The World's Most Misunderstood Programming Language[
^],
The World's Most Misunderstood Programming Language by Douglas Crockford — YOW! 2013 | Eventer[
^].
—SA