Of course, I don’t know if you wanted to use only stuff that you already covered before, or present (i.e., teach to) code that is on the one hand not so hard to understand but on the other not the very naive approach as well.
I would suggest an optimized (as well as condensed) function.
Here’s the original one:
function updateIdStr( value ) {
var strArray = value.split( '-' ),
id = strArray[ strArray.length - 1 ],
newId = parseInt( id ) + 1;
strArray[ strArray.length - 1 ] = newId;
return strArray.toString().replace( ',' , '-' );
}
And here’s the optimized one:
function updateIdStr( value ) {
var strArray = value.split( '-' );
strArray[ strArray.length - 1 ]++;
return strArray.join( '-' );
}
Less variables, less (unnecessary) calculcations, but one on-the-fly incrementing.
New is the usage of Array.prototype.join()
.