Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Making text singular or plural based on variable length

I am trying to make text singular or plural based on the value of templateCount. If templateCount's value is 1 then I just want 'Template' to say that, but if templateCount is plural, I want it to say 'Templates Selected'.

What am I doing wrong with my code?

$('#templateCount').html(templateCount + " Templates Selected" + templateCount.length == 1 ? "" : "s");
like image 790
Paul Avatar asked Jan 25 '26 16:01

Paul


1 Answers

Are you sure you thought this through? You have the string "Templates selected" there and you are conditionally appending s to the end of that (which would make it "Templates Selecteds").

Do this:

$('#templateCount').html(templateCount + 
    " Template" + 
    (templateCount === 1 ? "" : "s") +
    " Selected");
like image 77
JLRishe Avatar answered Jan 27 '26 04:01

JLRishe



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!