简体   繁体   English

在 Javascript 中制作复数值(大于、小于)

[英]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:我试图让它 output 像这样:

Clicked once: "I cheated 1 whole time."点击一次:“我作弊了 1 次。”

Clicked more than once: "I cheated X whole times."多次点击:“我作弊了 X 次。”

-- With an 's' at the end of "times". ——“times”结尾加一个“s”。

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!谢谢!

Here is your problem: total_click = 1 .这是您的问题: total_click = 1 Try changing it to total_click == 1 .尝试将其更改为total_click == 1 I don't see why you have that conditional in there however, as it won't work as you expect anyway.我不明白为什么你在那里有那个条件,因为它无论如何都不会像你期望的那样工作。 Try $("#counter").text("I cheated " + total_click + " whole time" + ((total_click == 1)? ' ': 's '));试试$("#counter").text("I cheated " + total_click + " whole time" + ((total_click == 1)? ' ': 's '));

It's okay to use suggested implementations for a trivial cases, however it will not scale for a bigger set of problems and will not work for multiple languages (or it will get ugly very fast).可以将建议的实现用于微不足道的情况,但是它不会针对更大的问题集进行扩展,并且不适用于多种语言(或者它会很快变得丑陋)。

With this in mind, I've created a very simple JavaScript library that can be used to pluralize words in almost any language.考虑到这一点,我创建了一个非常简单的 JavaScript 库,它可用于使几乎任何语言的单词复数。 It transparently uses CLDR database for multiple locales.它透明地将 CLDR 数据库用于多个语言环境。 It's API is very minimalistic and integration is extremely simple.它的 API 非常简约,集成非常简单。 It's called Numerous .它被称为无数

I've also written a small introduction article to it: « How to pluralize any word in different languages using JavaScript?我还写了一篇介绍它的小文章:«如何使用 JavaScript 使不同语言中的任何单词复数? ». »。

Feel free to use it in your project.随意在您的项目中使用它。 I will also be glad for your feedback on it!我也会很高兴收到您的反馈!

You are not using the ternary operator correctly, and also assigning total_click to 1 instead of checking its value.您没有正确使用三元运算符,并且还将 total_click 分配给 1 而不是检查其值。 I would suggest moving this to a function to simplify things.我建议将其移至 function 以简化操作。

function pluralize(singular, times) {
    if (times == 1) return singular;
    else return singular + 's';
}

Then change the string to然后将字符串更改为

var text = "I cheated " + clicks + " whole " + pluralize("time", clicks);

Here's an example .这是一个例子

  $(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;
          });
        });

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM