Yahreen Yahreen - 2 months ago 5
Javascript Question

Making a value plural (Greater than, Less than) in Javascript

I have the following code:

$(function(){
var total_click = 0;
$("#mapKey a.showKey").click(function(){
total_click = total_click + 1;
$("#counter").text("I cheated " + total_click + " whole" + (total_click = 1 ? + ' time' + ((total_click > 1) ? 's ' : ' ') : ''));
return false;
});
});


I'm trying to have it output as such:

Clicked once: "I cheated 1 whole time."

Clicked more than once: "I cheated X whole times."

-- With an 's' at the end of "times".

The counter is working fine, it's just the last part making the "time" or "times" show up appropriately that I am having difficulty with.

Any ideas what I'm doing wrong?

Thanks!

Answer
  $(function(){
          var total_click = 0;
          $("#mapKey a.showKey").click(function(){
            total_click = total_click + 1;
            $("#counter").text("I cheated " + total_click + " whole " + (total_click == 1 ? "time" : "times");
return false;
          });
        });
Comments