将单词“and”放在项目列表的文本中

Placing the word "and" in text for a list of items

本文关键字:列表 项目 文本 单词 and      更新时间:2023-09-26

嗨,这对所有人都是新的,但我制作了一个程序,您可以在其中订购披萨,它显示了我订购的内容,但我需要在最后一项末尾有一个"和"的地方制作它.....示例:您订购了一个带有手工面包皮的披萨。您有以下配料:意大利辣香肠、汉堡包、青椒蘑菇。 我需要那个并在那里,但似乎无法把它放在那里。任何帮助都会在这里做的是我的小程序

<!DOCTYPE html>
    <html lang="en" xmlns="http://www.w3.org/1999/xhtml">
    <head>
        <title>PIZZA!</title>
        <script type="text/javascript">
            function vOrder() {
                var strCrust, strOrder, iToppingCount, iMeatCount;
                document.getElementById("taOrder").value = "";
                strCrust = document.getElementById("ddlCrust").value;
                strOrder = "You ordered a pizza with " + strCrust + " crust.'n";
                iToppingCount = 0;
                iMeatCount = 0;
                strToppings = "";

                if (document.getElementById("chkPepperoni").checked) {
                    iToppingCount++;  //same as iToppingCount += 1
                    strToppings += "pepperoni";
                    iMeatCount++;
                }
                if (document.getElementById("chkHamburger").checked) {
                    if (iToppingCount > 0)
                        strToppings += ", ";
                    iToppingCount++;
                    strToppings += "hamburger";
                    iMeatCount++;
                }
                if (document.getElementById("chkGreenPeppers").checked) {
                    if (iToppingCount > 0)
                        strToppings += ", ";
                    iToppingCount++;
                    strToppings += "green peppers";
                }
                if (document.getElementById("chkMushrooms").checked) {
                    if (iToppingCount > 0)
                        strToppings += ", ";
                    iToppingCount++;
                    strToppings += "mushrooms";
                }
                if (document.getElementById("chkOnion").checked) {
                    if (iToppingCount > 0)
                        strToppings += ", ";
                    iToppingCount++;
                    strToppings += "onions";
                }
                if (document.getElementById("chkSausage").checked) {
                    if (iToppingCount > 0)
                        strToppings += ", ";
                    iToppingCount++;
                    strToppings += "sausage";
                    iMeatCount++;
                }

                if (iToppingCount > 0)
                    strToppings = "You have the following toppings: " + strToppings;
                else
                    strToppings = "Your pizza is a plain cheese.";
                if (iMeatCount > 2)
                    strOrder = "screw you no more then two";
                else
                    strOrder += strToppings;
                document.getElementById("taOrder").value = strOrder;
            }
        </script>
        <style type="text/css">
            hr {
                color:firebrick;
                height:4px;
            }
        </style>
    </head>

    <body>
        <div style="border-style:groove; border-color:firebrick; border-width:5px; margin:auto; width:4.5in; background-color:bisque">
            <form action="pizza_ddl_chk.html">
                <table style="width:100%">
                    <tr>
                        <td style="width:100px">
                            <img src="images/pizza.jpg" width="100" alt="Tasty pizza!" />
                        </td>
                        <td style="font-size:30pt;font-family:Biondi;text-align:center">
                            <span style="color:green">That's</span>  
                            <span style="color:white">A</span>
                            <span style="color:red">Pizza!</span>
                        </td>
                    </tr>
                    <tr>
                        <td colspan="2"><hr  /></td>
                    </tr>
                    <tr>
                        <td>Choose your crust:</td>
                        <td>
                            <select id="ddlCrust">
                                <option value="thin">Thin</option>
                                <option value="thick">Thick</option>
                                <option value="hand-tossed">Hand-tossed</option>
                            </select>
                        </td>
                    </tr>
                    <tr>
                        <td>Choose your toppings:</td>
                        <td>
                            <table>
                                <tr>
                                    <td>Pepperoni</td>
                                    <td>
                                        <input type="checkbox" id="chkPepperoni" />
                                    </td>
                                    <td>Sausage</td>
                                    <td>
                                        <input type="checkbox" id="chkSausage" />
                                    </td>
                                    <td>Onion</td>
                                    <td>
                                        <input type="checkbox" id="chkOnion" />
                                    </td>
                                </tr>
                                <tr>
                                    <td>Hamburger</td>
                                    <td>
                                        <input type="checkbox" id="chkHamburger" />
                                    </td>
                                    <td>Green Peppers</td>
                                    <td>
                                        <input type="checkbox" id="chkGreenPeppers" />
                                    </td>
                                    <td>Mushrooms</td>
                                    <td>
                                        <input type="checkbox" id="chkMushrooms" />
                                    </td>
                                </tr>
                            </table>
                        </td>
                    </tr>
                    <tr>
                        <td colspan="2" style="text-align:center"><input type="button" id="btnOrder" value="Place Your Order" onclick="vOrder()"/></td>
                    </tr>
                     <tr>
                        <td colspan="2"><hr  /></td>
                    </tr>
                    <tr>
                        <td colspan="2">
                            <textarea id="taOrder" cols="49" rows="4"></textarea>
                        </td>
                    </tr>
                </table>
            </form>
        </div>
    </body>
    </html>

将其分解为两部分。

首先,使用您需要的任何逻辑准备您的成分清单:

var ingredients = [];
ingredients.push("cheese");
ingreiendts.push("pepperoni");
ingredients.push("tomato");

然后创建句子:

var toppings;
if (ingredients.length == 0)
  toppings = "";
else if (ingredients.length == 1)
  toppings = ingredients[0];
else
  toppings = ingredients.slice(0, ingredients.length - 1).join(", ") + " and " + ingredients[ingredients.length - 1]

这:ingredients.slice(0, ingredients.length - 1).join(", ")将除最后一种成分以外的所有成分连接在一起,并带有", "作为分隔符。

" and " + ingredients[ingredients.length - 1]只是将最后一个成分添加到字符串上,中间有文本 ' 和 '

语法上讲,您必须将最后一个逗号替换为"and"。如果您已经构造了字符串,则只需替换其中最后一个出现的逗号即可。关于如何做到这一点有很多变化,这个版本使用这个答案的修改版本:

function replaceLastInstance(badtext, str, repl) {
    var charpos = str.lastIndexOf(badtext);
    if (charpos<0) return str;
    ptone = str.substring(0,charpos);
    pttwo = str.substring(charpos+(badtext.length));
    return (ptone+repl+pttwo);
}

使用此函数,您可以简单调用:

var str = 'You ordered a pizza with hand-tossed crust. You have the following toppings: pepperoni, hamburger, green peppers, mushroom.'
var str2 = replaceLastInstance(',', str, ' and')
console.log(str2);

结果是:

你点了一个手工拌面包皮的披萨。您有以下条件 配料:意大利辣香肠、汉堡包、青椒和蘑菇。

演示:http://jsfiddle.net/qVbn5/