[Guide] Basic JS: Profile lookup. Fixes and enhancements (#19115)
- Add title - Fixed HTML/markdown tags - Resources to bottom and in markdown - Deleted notes for contributors - Add JS highlighting to code example - Add alternative code solution and Run link
This commit is contained in:
committed by
Quincy Larson
parent
2ab6d429f5
commit
5e5c162303
@ -1,7 +1,8 @@
|
||||
---
|
||||
title: Profile Lookup
|
||||
---
|
||||
 Remember to use <a>**`Read-Search-Ask`**</a> if you get stuck. Try to pair program  and write your own code 
|
||||
## Profile Lookup
|
||||
 Remember to use `Read-Search-Ask` if you get stuck. Try to pair program  and write your own code 
|
||||
|
||||
###  Problem Explanation:
|
||||
|
||||
@ -26,10 +27,6 @@ If **prop** does not correspond to any valid properties then return `No such pro
|
||||
* If **firstName** is found and no associated **prop** is found, you should return `No such property`.
|
||||
* If **firstName** isn't found anywhere, you should return `No such contact`.
|
||||
|
||||
#### Relevant Links
|
||||
|
||||
* <a href='http://www.freecodecamp.com/challenges/accessing-objects-properties-with-bracket-notation' target='_blank' rel='nofollow'>Challenge: Accessing Objects Properties with Bracket Notation</a>
|
||||
* <a href='http://www.freecodecamp.com/challenges/iterate-with-javascript-for-loops' target='_blank' rel='nofollow'>Challenge: Iterate with JavaScript For Loops</a>
|
||||
|
||||
##  Hint: 1
|
||||
|
||||
@ -57,6 +54,8 @@ Leave your `return "No such contact"` out of the `for` loop as a final catch-all
|
||||
|
||||
##  Basic Code Solution:
|
||||
|
||||
|
||||
``` javascript
|
||||
for (var x = 0; x < contacts.length; x++){
|
||||
if (contacts[x].firstName === name) {
|
||||
if (contacts[x].hasOwnProperty(prop)) {
|
||||
@ -67,6 +66,7 @@ Leave your `return "No such contact"` out of the `for` loop as a final catch-all
|
||||
}
|
||||
}
|
||||
return "No such contact";
|
||||
```
|
||||
|
||||
### Code Explanation:
|
||||
|
||||
@ -84,11 +84,28 @@ Leave your `return "No such contact"` out of the `for` loop as a final catch-all
|
||||
* `"likes"` is found within the first object, so the second `if` statement returns true.
|
||||
* The value of `"likes"` is returned - `"Pizza", "Coding", "Brownie Points"`.
|
||||
|
||||
##  NOTES FOR CONTRIBUTIONS:
|
||||
## Alternative code solution:
|
||||
|
||||
*  **DO NOT** add solutions that are similar to any existing solutions. If you think it is **_similar but better_**, then try to merge (or replace) the existing similar solution.
|
||||
* Add an explanation of your solution.
|
||||
* Categorize the solution in one of the following categories — **Basic**, **Intermediate** and **Advanced**. 
|
||||
* Please add your username only if you have added any **relevant main contents**. ( **_DO NOT_** _remove any existing usernames_)
|
||||
```javascript
|
||||
for (var i = 0; i < contacts.length; i++){
|
||||
if (contacts[i].firstName === name){
|
||||
if (prop in contacts[i]){
|
||||
return contacts[i][prop];
|
||||
}
|
||||
else return "No such property";
|
||||
}
|
||||
}
|
||||
return "No such contact";
|
||||
}
|
||||
```
|
||||
· Run code at [repl.it](https://repl.it/@AdrianSkar/Basic-JS-Profile-lookup).
|
||||
|
||||
> See  <a>**`Wiki Challenge Solution Template`**</a> for reference.
|
||||
### Code explanation
|
||||
This works as the last example but uses the `in` operator to look for `prop` instead of the `hasOwnProperty()` method.
|
||||
|
||||
|
||||
### Resources
|
||||
|
||||
- ["Iterate with JavaScript For Loops" - *fCC's challenge*](https://learn.freecodecamp.org/javascript-algorithms-and-data-structures/basic-javascript/iterate-with-javascript-for-loops/)
|
||||
- ["Object.prototype.hasOwnProperty()" - *MDN JavaScript reference*](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/hasOwnProperty)
|
||||
- ["in operator" - *MDN JavaScript reference*](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/in)
|
||||
|
Reference in New Issue
Block a user