javascript - freecodecamp 不接受 JS 代码

标签 javascript arrays

我正在使用 freecodecamp 并进行以下练习:

https://learn.freecodecamp.org/javascript-algorithms-and-data-structures/basic-javascript/profile-lookup

现在我的代码如下:

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(name, prop) {
  // Only change code below this line
  // Only change code below this line

  for (var i = 0; i < contacts.length; i++) {
    if (contacts[i].firstName == name) {
      foundName += 1;
    }
    if (contacts[i].firstName == name && contacts[i].hasOwnProperty(prop)) {
      return contacts[i][prop];
    } else if (contacts[i].firstName == name && contacts[i].hasOwnProperty(prop) == false) {
      return "No such property";
    }
  }
  if (foundName < 1) {
    return "No such contact"
  };
}

var foundName = 0;

// Only change code above this line


// Change these values to test your function
var ans = lookUpProfile("Bob", "number");
console.log(ans);

因此,我使用 for 循环遍历预定义的数组,并检查 name == firstName 以及对象具有 prop 属性的实例。在这些情况下,我将归还属性(property)。否则,我返回“No such Property”。我还更改了变量foundName,以便当firstName 与循环中的名称匹配时,foundName 获得正值。如果foundName小于1(即没有找到匹配的名字),那么我返回“没有这样的联系人”。

现在,当我在浏览器中运行它并查看控制台时,它似乎工作得很好。然而,当我在 freecodecamp 中输入这个答案时,我得到:

“Bob”,“number”应返回“No such contact” “Bob”、“potato”应该返回“No such contact”

但是,例如,如果我将“Bob”和“number”放入函数中,我确实会得到“没有这样的联系”...我一定在这里遗漏了一些明显的东西,但我对此感到非常困惑!

最佳答案

问题出在变量foundName中,每次调用函数lookUpProfile时,都会更改全局变量foundName,因此当您调用该函数时** LookUpProfile** 以 'bob' 作为参数,它将返回 undefined 因为 foundName 的值大于 1 ,因此您需要将变量的范围限定为功能 block 作用域,您应该在函数内部而不是外部定义变量 foundName ,也停止使用关键字 var ,而是使用 let

看下面的解决方案

//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(name, prop) {
  // Only change code below this line
  // Only change code below this line
   let foundName = 0;
  for (var i = 0; i < contacts.length; i++) {
    if (contacts[i].firstName == name) {
      foundName += 1;
    }
    if (contacts[i].firstName == name && contacts[i].hasOwnProperty(prop)) {
      return contacts[i][prop];
    } else if (contacts[i].firstName == name && contacts[i].hasOwnProperty(prop) == false) {
      return "No such property";
    }
  }
  if (foundName < 1) {
    return "No such contact"
  };
}




// Only change code above this line


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

关于javascript - freecodecamp 不接受 JS 代码,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50642585/

相关文章:

javascript - Object.keys(KeyboardEvent) 只获取一个 "isTrusted"键而不是所有键

javascript - 跨浏览器背景Y位置动画?

php array_filter function not found or invalid function name 不管我输入什么

javascript - JavaScript 中的最小最大和 - 如何获取 5 元素数组中 4 个元素的最小和和最大和

c: a.exe 已停止工作

c# - 将固定大小的缓冲区(字节数组)转换为字符串

javascript - React.useEffect 在依赖项发生变化时不会触发

javascript - 如何使用 Rails 根据屏幕尺寸隐藏或显示 View 中的元素?

javascript - 替换indexOf的while

c# - 我如何通过Linq C#获得每个数组的第一个元素