Link to home
Start Free TrialLog in
Avatar of juan field
juan field

asked on

how can apply the following conditionals:

function getElementOfArrayProperty(obj, key, index) {
  if(obj.key.length === 0 || index > obj.key[index].length || typeof key[index] !== Array || !obj.key[index] ){
      return undefined;
  }
  return obj.key[index];
}

var obj = {
 key: ['Jamil', 'Albrey']
};
var output = getElementOfArrayProperty(obj, 'key', 0);
console.log(output); // --> 'Jamil'


////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////


Write a function called "getElementOfArrayProperty".

Given an object, a key, and a numerical index, "getElementOfArrayProperty" returns the value of the element at the given index of the array located within the given object at the given key.

Notes:
* If the array is empty, it should return undefined.
* If the given index is out of range of the array located at the given key, it should return undefined.
* If the property at the given key is not an array, it should return undefined.
* If there is no property at the key, it should return undefined.

var obj = {
 key: ['Jamil', 'Albrey']
};
var output = getElementOfArrayProperty(obj, 'key', 0);
console.log(output); // --> 'Jamil'

Starter Code :
function getElementOfArrayProperty(obj, key, index) {
  // your code here
}
ASKER CERTIFIED SOLUTION
Avatar of Member_2_248744
Member_2_248744
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Julian Hansen
I think that code will throw an error. You probably are looking for something along these lines
function getObjectArrayElement(obj, key, index) {
  // Check if this object has the desired key
  if (obj.hasOwnProperty(key)) {
    // Ensure the property is an array
    if (obj[key].constructor == Array) {
      // You can probably leave out this test - it will
      // be implied by trying to return a non-index
      // Included here to show intent
      if (typeof obj[key][index] != undefined) {
        return obj[key][index];
      }
    }
  }
  // Return undefined for anything that is not valid
  return undefined;
} 

Open in new window

Avatar of juan field
juan field

ASKER

// this is how i did it :

var obj = {
 key: ['Jamil', 'Albrey']
};
 function getElementOfArrayProperty(obj, key, index) {
   if ( obj[key] === undefined || obj[key].length < index || Array.isArray(obj[key]) === false  ){
       return undefined;
   } else {
 return obj[key][index];
   }
}
var output = getElementOfArrayProperty(obj, 'key', 0);
console.log(output); // --> 'Jamil'
@juan field  ,  ,  , just note for programming syntax You do NOT need the conditional
     if - else
if you use the line by line sequence ESCAPE of -
   return

function getElementOfArrayProperty(obj, key, index) {
   if ( obj[key] === undefined || obj[key].length < index || Array.isArray(obj[key]) === false  ){
       return undefined; // the "return" here ENDS the progression of this function
   } //else { // so you DO NOT need the else here
 return obj[key][index];
//   }
}

Open in new window


also in my tests since the the function  obj  parameter must be defined to continue execution, then I found this to work for any and all incorrect parameters -

function getElementOfArrayProperty(obj, key, index) {
  if (!obj[key]) return undefined;
  return obj[key][index];
}

var aryObj =[
  "low",
  {},
  {joke: [1,2]},
  {ok: 7},
  {ok: {dog:'food'}},
  {ok: ['Zero','One','Two']}
];

console.log("loop result of Objects Test\n");
for (var i = 0; i < aryObj.length; ++i) {
  console.log(getElementOfArrayProperty(aryObj[i], 'ok', 0)+" - "+i+"\n");
}

Open in new window


= = = = = = = = =

it is also "misleading" and "confusing" for developers to name parameters AND Globals with the same "Name" as you did with your Global of "obj" and your parameter of "obj"
A note to anyone reading the thread - the accepted answer includes this code
function getObjectArrayElement(obj, key, index) {
  return obj[ key ][ index ];
} 

Open in new window

Which is incorrect - this code does not solve the problem - it throws a Syntax error and does not return undefined as stated in the requirements