Ah, yes, adding 1 to it throws it right off, since it ends up on 3 which should never happen. That'll teach me to show off by breaking KISS 
Code:
<script type="text/javascript">
var nextEl = (function(n) {
var ordinals = {
'1' : 'first',
'2' : 'second',
'3' : 'third'
};
var i = 1;
function nextEl() {
alert("c" + i + " - " + ordinals[i] + " element");
i = i > n ? 1 : i + 1;
}
return nextEl;
})(3);
</script>
You can make it do more by increasing the 3 at the bottom.
Anyway, yes, the maths I used to try to get it to cycle isn't really important (maths has never been my strong point
) and you can do it with an if as you attempted (the above being a condensed, less redundant version of such):
Code:
function nextEl() {
alert("c" + i + " - " + ordinals[i] + " element");
if (i > n)
i = 1
else
++i;
}
The strange behaviour you encountered was because you wrapped your numbers in quotation marks, making them into strings. 1 + 1 === 2, but '1' + '1' === '11'.
Bookmarks