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
}
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(
console.log(output); // --> 'Jamil'
//////////////////////////
//////////////////////////
//////////////////////////
Write a function called "getElementOfArrayProperty
Given an object, a key, and a numerical index, "getElementOfArrayProperty
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(
console.log(output); // --> 'Jamil'
Starter Code :
function getElementOfArrayProperty(
// your code here
}
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
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'
var obj = {
key: ['Jamil', 'Albrey']
};
function getElementOfArrayProperty(
if ( obj[key] === undefined || obj[key].length < index || Array.isArray(obj[key]) === false ){
return undefined;
} else {
return obj[key][index];
}
}
var output = getElementOfArrayProperty(
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
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 -
= = = = = = = = =
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"
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];
// }
}
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");
}
= = = = = = = = =
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 ];
}
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
Open in new window