【问题标题】:Alert() not working correctly in else statementAlert() 在 else 语句中无法正常工作
【发布时间】:2020-01-10 09:38:30
【问题描述】:

当我在我的用户输入中输入一个与类型 obj 不匹配的随机字符串时,会出现警报。但是,当我输入与警报(无效类型)匹配的有效输入时,仍然会弹出。并且不通过 if 语句。

示例: 用户输入:作为有效输入的恐怖应该返回所有电影列表,但警报仍然出现,说它不是有效的类型。

else 语句出现 if 语句是否等于genre[i].name

$('#submitButton').click(function(){

          reset();

          // getting genre from user
          let genreSubmission = $('#inputSearch').val().toLowerCase();
          let genreId = 0;


          // api genre ids objects to change the api link
          const genre = [
            {
              "id": 28,
              "name": "Action"
            },
            {
              "id": 12,
              "name": "Adventure"
            },
            {
              "id": 16,
              "name": "Animation"
            },
            {
              "id": 35,
              "name": "Comedy"
            },
            {
              "id": 80,
              "name": "Crime"
            },
            {
              "id": 99,
              "name": "Documentary"
            },
            {
              "id": 18,
              "name": "Drama"
            },
            {
              "id": 10751,
              "name": "Family"
            },
            {
              "id": 14,
              "name": "Fantasy"
            },
            {
              "id": 36,
              "name": "History"
            },
            {
              "id": 27,
              "name": "Horror"
            },
            {
              "id": 10402,
              "name": "Music"
            },
            {
              "id": 9648,
              "name": "Mystery"
            },
            {
              "id": 10749,
              "name": "Romance"
            },
            {
              "id": 878,
              "name": "Science Fiction"
            },
            {
              "id": 10770,
              "name": "TV Movie"
            },
            {
              "id": 53,
              "name": "Thriller"
            },
            {
              "id": 10752,
              "name": "War"
            },
            {
              "id": 37,
              "name": "Western"
            }];

          for (let index = 0; index < genre.length; index++) {
              if(genreSubmission === genre[index].name.toLowerCase()){
                genreId = genre[index].id;
                console.log(genreId);
              } else{
                return alert("not a valid genre");
              }
          };

【问题讨论】:

  • 您检查每一个类型,因此如果用户键入有效的“恐怖”,您仍然会收到array.length-1 警报,每个类型都显示“无效”其余类型。

标签: javascript for-loop if-statement


【解决方案1】:

假设您键入 Adventure

  1. 你进入循环,index0
  2. genre[0].nameAction 而不是 Adventure
  3. 所以你return alert("not a valid genre");
    • 警报
    • 退出函数(因为这是return所做的)

自从您退出循环后,index 永远不会增加到 1,您也永远找不到匹配项。


您需要搜索整个数组而不找到匹配项,然后放弃并提醒并返回。

虽然您可以使用for 循环来做到这一点,但arrays have a built-in method for doing this

const match = genre.find( 
    element => element.name.toLowerCase() === genreSubmission 
);
if (match) {
    console.log(match.id);
} else {
    return alert("not a valid genre");
}

【讨论】:

  • 啊,我现在看到了循环的逻辑。
猜你喜欢
  • 2015-01-01
  • 2016-06-18
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-09-14
  • 1970-01-01
  • 2014-09-27
  • 1970-01-01
相关资源
最近更新 更多