简体   繁体   English

嵌套在 for 循环外的 return 语句出现问题

[英]Issue with a return statement nested outside a for loop

I amm new to javascript and am working through freecodecamp's resources (amongst other things).我是 javascript 的新手,并且正在研究 freecodecamp 的资源(除其他外)。 There was a challenge task to loop through phone contacts and I am sure some people will be familiar with it.遍历电话联系人是一项具有挑战性的任务,我相信有些人会熟悉它。 I managed the task just fine, but am confused as to why the final return statement needs to exist outside the for loop, as opposed to an else statement for the outer if.我很好地完成了任务,但是对于为什么最终的 return 语句需要存在于 for 循环之外而不是外部 if 的 else 语句感到困惑。

function lookUpProfile(name, prop){
  for (var i = 0; i < contacts.length; i++) {
    if (name === contacts[i].firstName) {
      if (prop === "lastName" || prop === "number" || prop === "likes") {
        return contacts[i][prop];
      }else{
        return "No such property"
      };
    }    
  }
  return "No such contact";
};

If I ever nested the final return statement (as an else) inside the for loop, the code would appear to go straight to this, even though the conditions for the initial if statement were true.如果我曾经在 for 循环中嵌套了最终的 return 语句(作为 else),即使初始 if 语句的条件为真,代码也会直接出现在 go 中。

Can anyone please explain why?谁能解释一下为什么? If you need any further information please let me know.如果您需要任何进一步的信息,请告诉我。 I have tried searching google for why this might be the case but I haven't managed to find anything.我曾尝试在谷歌上搜索为什么会出现这种情况,但我没有找到任何东西。

Thanks!谢谢!

You don't wanna return "No such contact" just because comparing first element returns false.您不想仅仅因为比较第一个元素返回错误就返回“没有这样的联系”。 This will not work.这行不通。 Only after comparing and checking for all the elements in array you would know the contact doesn't exists.只有在比较和检查数组中的所有元素之后,您才会知道联系人不存在。 If the first contact doesn't match loop will return and no further comparisons will be done.如果第一个联系人不匹配,则循环将返回,并且不会进行进一步的比较。

I've added an else to your example to be more explicit to hopefully make it more clear what is actually happening in the for loop.我在您的示例中添加了一个 else 以更加明确,希望能够更清楚地说明 for 循环中实际发生的情况。 That else statement doesn't need to be there because the for loop would continue regardless. else 语句不需要存在,因为 for 循环无论如何都会继续。

function lookUpProfile(name, prop){
    for (var i = 0; i < contacts.length; i++) {
        if (name === contacts[i].firstName) {
            if (prop === "lastName" || prop === "number" || prop === "likes") {
                return contacts[i][prop];
            }
            else return "No such property";
        } else {
            continue;
        }  
    }
    return "No such contact";
};

If you were to place a return where the continue currently is you would never make it past the first contact.如果您要在当前continue的地方放置return ,您将永远不会超过第一次接触。

You need to iterate over all the elements to make sure that there is no contact with matching name.您需要遍历所有元素以确保没有匹配名称的联系人。

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

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