简体   繁体   English

如何在JavaScript中检查字符串是否相等

[英]How can I check string to equality in JavaScript

I'm nearly new to JavaScript. 我几乎不熟悉JavaScript。

I want to make a random-name generator where from a string array I get 5 random names. 我想制作一个随机名称生成器,从字符串数组中获取5个随机名称。 This works, but I want to add many names, and I want to check that no names are duplicated. 这可行,但是我想添加许多名称,并且我想检查是否没有名称重复。 Because of that, I wanted to make a textfield where I can write names, and with the button press a for-loop looks after the array index, and with an if-statement. 因此,我想创建一个文本字段,在其中可以写名称,并通过按钮按下for循环来监视数组索引,并使用if语句。 T Ť

his is the html code: 他的是html代码:

<div id="box">
    <p id="first"></p>
    <p id="second"></p>
    <p id="third"></p>
    <p id="fourth"></p>
    <p id="fifth"></p>
</div>
<br>
<input type="button" value="Generier Namen" id="button">

<div>
    <input type="text" placeholder="Name.." id="input">
    <input type="button" value="Checken" id="checkbutton">
    <p id="output"></p>
</div>

and this the JavaScript 这是JavaScript

let namesMale = ["Akemi", "Akechi", "Akihiko", "Akihiro", "Akira", "Amida", "Arata", "Daisuke", "Eichi", "Eiko", "Hajime", "Haru", "Haruo", "Hikaru", "Hiroshi", "Hiroyuki", "Hisoka", "Hoshi", "Isao", "Izanagi", "Kaori", "Kazuhiko", "Kenji", "Kentaro", "Kichi", "Kioshi", "Kitaro", "Kiyo", "Kiyoshi", "Koshiro", "Masahiro", "Masayuki", "Minori", "Mitsuo", "Namiyo", "Naoko", "Nikko", "Ozuru", "Raidon", "Renjiro", "Ryo", "Ryuji", "Sadao", "Satoshi", "Seiichi", "Shinichi", "Shuji", "Taiyo", "Takahiro", "Takeo", "Takiyo", "Taku", "Tama", "Tatsuo", "Tenchi", "Tetsuya", "Tomo", "Torio", "Toshi", "Toyo", "Tsutomu", "Yasashiku", "Yoshi", "Yoshikazu", "Yoshimitsu", "Yoshinori", "Yoshito", "Yoshiyuki", "Yuji", "Yuki", "Yukio", "Yutaka", "Yuudai", "Zinan"];

    document.getElementById("button").onclick = function() {
        let randomOne = Math.floor(Math.random() * namesMale.length);
        let randomTwo = Math.floor(Math.random() * namesMale.length);
        if(randomTwo == randomOne){
            let randomTwo = Math.floor(Math.random() * namesMale.length);
        }
        let randomThree = Math.floor(Math.random() * namesMale.length);
        if(randomThree == randomOne ||randomThree == randomTwo){
            let randomThree = Math.floor(Math.random() * namesMale.length);
        }
        let randomFour = Math.floor(Math.random() * namesMale.length);
        if(randomFour == randomOne || randomFour == randomTwo || randomFour == randomThree){
            let randomFour = Math.floor(Math.random() * namesMale.length);
        }
        let randomFive = Math.floor(Math.random() * namesMale.length);
        if(randomFive == randomOne || randomFive == randomTwo || randomFive == randomThree || randomFive == randomFour){
            let randomFive = Math.floor(Math.random() * namesMale.length);
        }

        document.getElementById("first").innerHTML = namesMale[randomOne];
        document.getElementById("second").innerHTML = namesMale[randomTwo];
        document.getElementById("third").innerHTML = namesMale[randomThree];
        document.getElementById("fourth").innerHTML = namesMale[randomFour];
        document.getElementById("fifth").innerHTML = namesMale[randomFive];
    }



    document.getElementById("checkbutton").onclick = function() {
        let input = document.getElementById("input").value;
        for(let i = 0; i <= namesMale.length; i++){
            console.log(namesMale[i]);
            if(input.equals(namesMale[i])){
                document.getElementById("output").innerHTML = "Existiert";
            }
            else{
                document.getElementById("output").innerHTML = "Existiert nicht";
            }
        }
    }


//  ####### Event Handler ####### 

    let pressTab = document.getElementById("button");
    pressTab.addEventListener("keyup", function(event){
        if(event.keyCode === 13){
            event.preventDefault();
            document.getElementById("set").click();
        }
    });

The way I want it to work works in C# and Java, but my problem is, that the chrome console says: Uncaught TypeError: input.equals is not a function 我希望它的工作方式可以在C#和Java中工作,但是我的问题是chrome控制台说:Uncaught TypeError:input.equals不是一个函数

How can I make it works, because in the documentation I found nothing.. 我如何使它起作用,因为在文档中我什么都没找到。

Edit: I forgot to say that "===" and "==" will get me (if the name is in the list or not) "Existiert nicht" in english "don't exist" 编辑:我忘了说“ ===”和“ ==”会让我(无论姓名是否在列表中)“ Existiert nicht”用英语“不存在”

You can use == to check the equality 您可以使用==检查是否相等

 let namesMale = ["Akemi", "Akechi", "Akihiko", "Akihiro", "Akira", "Amida", "Arata", "Daisuke", "Eichi", "Eiko", "Hajime", "Haru", "Haruo", "Hikaru", "Hiroshi", "Hiroyuki", "Hisoka", "Hoshi", "Isao", "Izanagi", "Kaori", "Kazuhiko", "Kenji", "Kentaro", "Kichi", "Kioshi", "Kitaro", "Kiyo", "Kiyoshi", "Koshiro", "Masahiro", "Masayuki", "Minori", "Mitsuo", "Namiyo", "Naoko", "Nikko", "Ozuru", "Raidon", "Renjiro", "Ryo", "Ryuji", "Sadao", "Satoshi", "Seiichi", "Shinichi", "Shuji", "Taiyo", "Takahiro", "Takeo", "Takiyo", "Taku", "Tama", "Tatsuo", "Tenchi", "Tetsuya", "Tomo", "Torio", "Toshi", "Toyo", "Tsutomu", "Yasashiku", "Yoshi", "Yoshikazu", "Yoshimitsu", "Yoshinori", "Yoshito", "Yoshiyuki", "Yuji", "Yuki", "Yukio", "Yutaka", "Yuudai", "Zinan"]; function findName(val) { for (let i = 0; i <= namesMale.length; i++) { console.log(namesMale[i]); if (val == namesMale[i]) { console.log("Existiert"); } else { console.log("Existiert nicht"); } } } findName('Yuki'); 

You can also use a function indexOf to find if particular string is present in an array 您还可以使用函数indexOf来查找数组中是否存在特定的字符串

 let namesMale = ["Akemi", "Akechi", "Akihiko", "Akihiro", "Akira", "Amida", "Arata", "Daisuke", "Eichi", "Eiko", "Hajime", "Haru", "Haruo", "Hikaru", "Hiroshi", "Hiroyuki", "Hisoka", "Hoshi", "Isao", "Izanagi", "Kaori", "Kazuhiko", "Kenji", "Kentaro", "Kichi", "Kioshi", "Kitaro", "Kiyo", "Kiyoshi", "Koshiro", "Masahiro", "Masayuki", "Minori", "Mitsuo", "Namiyo", "Naoko", "Nikko", "Ozuru", "Raidon", "Renjiro", "Ryo", "Ryuji", "Sadao", "Satoshi", "Seiichi", "Shinichi", "Shuji", "Taiyo", "Takahiro", "Takeo", "Takiyo", "Taku", "Tama", "Tatsuo", "Tenchi", "Tetsuya", "Tomo", "Torio", "Toshi", "Toyo", "Tsutomu", "Yasashiku", "Yoshi", "Yoshikazu", "Yoshimitsu", "Yoshinori", "Yoshito", "Yoshiyuki", "Yuji", "Yuki", "Yukio", "Yutaka", "Yuudai", "Zinan"]; function findName(val) { if (namesMale.indexOf(val) != -1) { console.log(val+" Existiert"); } else { console.log(val+" Existiert nicht"); } } findName('Yuki'); findName('ABC'); 

String has no such method equals . String没有这样的equals方法。 Neither you have defined on its prototype. 您都尚未定义其原型。 You could check rather like this: 您可以像这样检查:

if(input == namesMale[i]){

Or, 要么,

if(input === namesMale[i]){ // strictly compare

As per error mentioned in question you are facing error below 根据有问题的错误,您在下面遇到错误

if(input.equals(namesMale[i])){  //Error is here, javascript does not contain .equal method
                document.getElementById("output").innerHTML = "Existiert";
            }
            else{
                document.getElementById("output").innerHTML = "Existiert nicht";
            }

You can try with 您可以尝试

if(input === namesMale[i]) //=== triple equal to check type and value 

or 要么

 if(input == namesMale[i]) //== double equal for testing loose equality

Unfortunately, there is no equals function for strgins comparison in JavaScript. 不幸的是,JavaScript中没有用于均等比较的equals函数。 So, instean of that you can check if these two strings are equal with === operator. 因此,您可以通过===运算符检查这两个字符串是否相等。 Overall, your function must look like: 总体而言,您的函数必须如下所示:

document.getElementById("checkbutton").onclick = function() {
    let input = document.getElementById("input").value;
    for(let i = 0; i <= namesMale.length; i++){
        console.log(namesMale[i]);
        if(input === namesMale[i]){
            document.getElementById("output").innerHTML = "Existiert";
        }
        else{
            document.getElementById("output").innerHTML = "Existiert nicht";
        }
    }
}

Hope it helped, Cheers! 希望能有所帮助,干杯!

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

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