循环遍历数组,但即使“if”语句为真,也执行“else”语句(javascript)

时间:2016-07-08 10:18:31

标签: javascript loops object if-statement

在循环中,我不确定是否犯了错误或错误的代码。当我单独运行if语句时,它可以工作。但是当我将它与“else if”语句一起运行时。 if语句失败,else if语句有效,即使第一个if语句为真。

https://www.freecodecamp.com/challenges/profile-lookup

//Setup
var contacts = [
{
    "firstName": "Akira",
    "lastName": "Laine",
    "number": "0543236543",
    "likes": ["Pizza", "Coding", "Brownie Points"]
},
{
    "firstName": "Harry",
    "lastName": "Potter",
    "number": "0994372684",
    "likes": ["Hogwarts", "Magic", "Hagrid"]
},
{
    "firstName": "Sherlock",
    "lastName": "Holmes",
    "number": "0487345643",
    "likes": ["Intriguing Cases", "Violin"]
},
{
    "firstName": "Kristian",
    "lastName": "Vos",
    "number": "unknown",
    "likes": ["Javascript", "Gaming", "Foxes"]
}
];


function lookUpProfile(firstName, prop){
// Only change code below this line
for (var i = 0; i < contacts.length; i++) {
  if ( contacts[i].firstName === firstName  &&     contacts[i].hasOwnProperty(prop) ) {
    return contacts[i][prop];


}  else if( contacts[i].firstName !== firstName ) {
  return "No such contact";  
    }
  }
// Only change code above this line
}

// Change these values to test your function
lookUpProfile("Akira", "likes");

3 个答案:

答案 0 :(得分:1)

移动&#34;没有这样的联系&#34;在循环外回复:

function lookUpProfile(firstName, prop) {
  var match = false;
  for (var i = 0; i < contacts.length; i++) {
    if (contacts[i].firstName === firstName) {
      match = true;
      var valid = contacts[i].hasOwnProperty(prop);
    }
    if (match && valid) {
      return contacts[i][prop];
    }
    if (match && !valid) {
      return "No such property";
    }
  }
  return "No such contact";  
}

假设:每个人都存储在一张卡片中。

此代码迭代所有联系人,当当前联系人符合条件时,将立即返回。当循环结束时,表示没有匹配的联系人,因此返回相应的回复。

我更喜欢这种没有嵌套if的编码方式,但您可以像这样重写内部for代码:

if (contacts[i].firstName === firstName) {
  if (contacts[i].hasOwnProperty(prop)) {
    return contacts[i][prop];
  } else {
    return "No such property";
  }
}

答案 1 :(得分:0)

@ Max答案的替代方案:

想法是拥有一个将被返回的变量。现在,在每种情况下,您只需设置此变量值即可。这将使您能够添加额外的逻辑,如getContactmassageContact,如果存在重复值,合并它们或获取特定值等。

function lookUpProfile(firstName, prop) {
  var match = undefined;
  // Only change code below this line
  for (var i = 0; i < contacts.length; i++) {
    if (contacts[i].firstName === firstName && contacts[i].hasOwnProperty(prop)) {
      match = contacts[i][prop];
      break;
    }
  }
  // Do extra stuff here
  return match ? match : "No such contact"
}

替代array.find。您还可以查看array.filter

Array.find

function lookUpProfile(firstName, prop){
  var o = contacts.find(function(c){ return c.firstName === firstName; });
  if(o && o.hasOwnProperty(prop)) return o[prop];
  else if(o && !o.hasOwnProperty(prop)) return "No such property";
  else return "No such contact";
}

参考文献:

注意:在使用它们之前,请检查它们的兼容性。

答案 2 :(得分:0)

Max Zuber是对的,您的代码如下:

With If else statement:
for (var i = 0; i < contacts.length; i++) {

    // if this true, the inside code block will be executed. 
    // You have return statement so, the loop stops and return the data. 
    if ( contacts[i].firstName === firstName  &&       contacts[i].hasOwnProperty(prop) ) {

        return contacts[i][prop];

    // if above fails, it will check this condition. 
    // If this true(which on   your design is always true) the inside code block is executed. 
    // You have return statement so the loop stops and return the message. 
    }  else if( contacts[i].firstName !== firstName ) {
     return "No such contact";  
      }
}

With If statement alone:
for (var i = 0; i < contacts.length; i++) {

    // If this true, stop the loop and return the data. 
    // else, loop continue until no data to be check. 
    if ( contacts[i].firstName === firstName  &&     contacts[i].hasOwnProperty(prop) ) {

      return contacts[i][prop];
      } 
}

// This will only be executed if the above if statement fails. 
return "No such contact";  

注意: 当函数命中return语句时,它将停止执行并传递return语句,即使有数据等待检查if else语句会发生什么。