From ad3d0aed04d40824cd2a532f36fdfeb01f39456d Mon Sep 17 00:00:00 2001 From: Randell Dawson <5313213+RandellDawson@users.noreply.github.com> Date: Tue, 28 May 2019 13:04:08 -0700 Subject: [PATCH] fix: corrected guide solutions and explanations (#36125) --- .../index.md | 60 ++++++++++++++----- 1 file changed, 44 insertions(+), 16 deletions(-) diff --git a/guide/english/certifications/javascript-algorithms-and-data-structures/basic-data-structures/check-if-an-object-has-a-property/index.md b/guide/english/certifications/javascript-algorithms-and-data-structures/basic-data-structures/check-if-an-object-has-a-property/index.md index b47cf95fe8..10653fca0e 100644 --- a/guide/english/certifications/javascript-algorithms-and-data-structures/basic-data-structures/check-if-an-object-has-a-property/index.md +++ b/guide/english/certifications/javascript-algorithms-and-data-structures/basic-data-structures/check-if-an-object-has-a-property/index.md @@ -3,13 +3,7 @@ title: Check if an Object has a Property --- ## Check if an Object has a Property -Method: -- The simplest way to complete this challenge is to create an `ìf-statement` to check wether or not the object contains all users, then to return a true or false statement. The first solution does just this. -- The second solution works in exactly the same way, only it uses 1 line of code - `Conditional(ternary)-Operator` - within the function. - -[developer.mozilla.org](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Conditional_Operator) provides a more in depth analysis of the ternary operator. - -### Solution-1: +### Basic Solution: ```javascript let users = { @@ -32,23 +26,57 @@ let users = { }; function isEveryoneHere(obj) { - // change code below this line - if(users.hasOwnProperty('Alan','Jeff','Sarah','Ryan')) { + if ( + obj.hasOwnProperty('Alan') && obj.hasOwnProperty('Jeff') && + obj.hasOwnProperty('Sarah') && obj.hasOwnProperty('Ryan') + ) { return true; } return false; - // change code above this line } - -console.log(isEveryoneHere(users)); - ``` -### Solution-2: +#### Code Explanation + +* Checks whether object contains all users by using the `hasOwnProperty` method for each name with the `&&` operator to return a `true` or `false` value. + +### Advanced Solution: ```javascript +let users = { + Alan: { + age: 27, + online: true + }, + Jeff: { + age: 32, + online: true + }, + Sarah: { + age: 48, + online: true + }, + Ryan: { + age: 19, + online: true + } +}; function isEveryoneHere(obj) { - return (users.hasOwnProperty('Alan','Jeff','Sarah','Ryan')) ? true : false; + return [ + 'Alan', + 'Jeff', + 'Sarah', + 'Ryan' + ].every(name => obj.hasOwnProperty(name)); } - ``` + +#### Code Explanation + +* Uses an array with all of the names which should be present in the object. +* The `every` method is used to validate all of names used in conjunction with the `hasOwnProperty` method result in a value of `true` being returned during each iteration. +* If at least one name is not found using the `hasOwnProperty` method, the `every` method returns `false`. + +#### Relevant Links + +* [Array.prototype.every](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/every)