簡體   English   中英

JS代碼未在freecodecamp中接受

[英]JS code not being accepted in freecodecamp

我正在通過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屬性的實例。 在這些情況下,我將退還財產。 否則,我返回“沒有此類屬性”。 我還更改了變量foundName,以便在循環中將firstName與name匹配時,foundName將獲得正值。 如果foundName小於1(即未找到匹配的名稱),則返回“沒有此類聯系人”。

現在,當我在瀏覽器中運行此程序並在控制台中查看時,它似乎運行良好。 但是,當我在freecodecamp中輸入此答案時,我得到:

“鮑勃”,“號碼”應返回“沒有此類聯系”“鮑勃”,“馬鈴薯”應返回“沒有此類聯系”

但是,如果我在函數中輸入了“ Bob”和“ number”,我會得到“沒有這種聯系”……我肯定在這里遺漏了一些明顯的東西,但是對此我感到非常困惑!

問題出在每次調用函數lookUpProfile時,您的變量foundName都會更改全局變量foundName,因此當您以'bob'作為參數調用函數** lookUpProfile **時,它將返回未定義狀態,因為foundName的值大於1因此,您需要將變量的作用域限定為功能塊作用域,應在函數內部而不是函數外部定義變量findName ,還應停止使用關鍵字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"); 

您的對象聲明不正確: contacts是一個數組。

該函數有效,但是您需要將foundName包含在函數中而不是外部。 否則,如果您兩次運行函數,它將中斷。

這是工作代碼:

 const 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) { 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" }; } console.log(lookUpProfile("Kristian", "lastName")) console.log(lookUpProfile("Bob", "number")) 

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM