0

I'm nearly new to JavaScript.

I want to make a random-name generator where from a string array I get 5 random names. 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. T

his is the html code:

<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

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

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"

Traijan
  • 73
  • 1
  • 8
  • 3
    *Shuffle* the array, take the first *n* names from it. Much simpler and saner than what you’re doing. – deceze Mar 24 '19 at 09:17
  • To add to @deceze's brilliant answer, here's how to shuffle the array: https://stackoverflow.com/questions/2450954/how-to-randomize-shuffle-a-javascript-array – frhd Mar 24 '19 at 09:24
  • You are checking *all* names in the array, and for *every* name you set the output as “existiert” or “existiert nicht”. So unless it’s exactly the last name you’re checking, you’ll overwrite any “existiert” in the next loop iteration. But further, you’re looping one more than the array is long, so the last item will never match and you’ll always end up with “existiert nicht”. – deceze Mar 24 '19 at 09:24
  • 1
    You should also be running into issues with your `let` inside `if` blocks; those variables aren’t accessible outside the `if` block. – deceze Mar 24 '19 at 09:27

4 Answers4

1

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

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');
Syed Mehtab Hassan
  • 1,297
  • 1
  • 9
  • 23
0

String has no such method 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
Bhojendra Rauniyar
  • 83,432
  • 35
  • 168
  • 231
0

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
Prasad Telkikar
  • 15,207
  • 5
  • 21
  • 44
0

Unfortunately, there is no equals function for strgins comparison in JavaScript. 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!

syntax-punk
  • 3,736
  • 3
  • 25
  • 33
  • sorry, I forgot to say that with "===" and "==" I get as output from the if statement "Existiert nicht" -> "don't exist" even though the name is in the array (copied) and I don't know why – Traijan Mar 24 '19 at 09:32