我几乎不熟悉JavaScript。
我想制作一个随机名称生成器,从字符串数组中获得5个随机名称。这可行,但是我想添加许多名称,并且我想检查是否没有名称重复。因此,我想创建一个文本字段,在其中可以写名称,并通过按钮按下for循环来监视数组索引,并使用if语句。 T
他是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>
这是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();
}
});
我希望它的工作方式可以在C#和Java中工作,但是我的问题是chrome控制台说:Uncaught TypeError:input.equals不是一个函数
如何使它起作用,因为在文档中我什么都没找到。
编辑:我忘了说“ ===”和“ ==”会让我(无论姓名是否在列表中)使我“ Existiert nicht”用英语“不存在”
答案 0 :(得分:1)
您可以使用==检查是否相等
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');
您还可以使用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');
答案 1 :(得分:0)
String没有这样的方法equals
。您都尚未定义其原型。您可以像这样检查:
if(input == namesMale[i]){
或者,
if(input === namesMale[i]){ // strictly compare
答案 2 :(得分:0)
根据有问题的错误,您在下面遇到错误
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";
}
您可以尝试
if(input === namesMale[i]) //=== triple equal to check type and value
或
if(input == namesMale[i]) //== double equal for testing loose equality
答案 3 :(得分:0)
不幸的是,JavaScript中没有equals
函数可用于strgins比较。因此,您可以通过===
运算符检查这两个字符串是否相等。总体而言,您的函数必须如下所示:
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";
}
}
}
希望会有所帮助,干杯!