this question has answer here:
- how remove last comma? 11 answers
i trying finish program, , ran issue can't quite figure out. want change array of numbers string of ordinal counter parts, have completed program, last value leave of concatenated ', ' (comma space). tried using if statement can't quite work.
function getgetordinal(n) { var s=["th","st","nd","rd"], v=n%100; return n+(s[(v-20)%10]||s[v]||s[0]); } var nums = [1, 2, 3, 4] var n = ""; ( var = 0; <= getgetordinal(4).length; i++ ) { if ( n < getgetordinal(n) ) { n += getgetordinal(nums[i]) + ', '; console.log(n); } } // ['1st, 2nd, 3rd, 4th'] < ~ expected result
here 2 options:
one way add check inside for
loop prepend comma if not first iteration of loop:
for (var i=0; <= getgetordinal(4).length; i++) { if (n < getgetordinal(n)) { if (i > 0) { n += ', '; } n += getgetordinal(nums[i]); } } console.log(n);
another option leave code is, substring off trailing comma:
for (var i=0; <= getgetordinal(4).length; i++) { if (n < getgetordinal(n)) { n += getgetordinal(nums[i]) + ', '; } } //n = n.substring(0, n.length - 2); n = n.slice(0, -2); console.log(n);
Comments
Post a Comment