Complete the hasElems function. This function should:
take two arguments. The first is an object and the second is an array of elements
return true when all of the elements in the array are keys in the object, false otherwise
// Here is my first run at hasElems
module.exports.hasElems = function hasElems (argObject, argArray)
{
var tempArray = Object.keys(argObject);
if (tempArray.length != argArray.length)
{
return false;
}
else
{
for (var i=0; i < argArray.length; i++)
{
if (tempArray[i] != argArray[i])
{
return false;
}
else
{
return true;
};
}
};
};
// here are my results
copy
✓ should be defined
✓ should create a new object with a copy of all the data
extend
✓ should be defined
✓ should correctly extends two hashes that are unique
✓ should correctly extend two hashes that are have keys in common
hasElems
✓ should be defined
1) returns true for an empty array
✓ returns true if it has all the keys
✓ returns false if one or more of the keys isn't in the hash
8 passing (14ms)
1 failing
1) hasElems returns true for an empty array:
AssertionError: expected false to be true
//Here is my modified code that 'passed' (although I feel it could be way more efficient)
module.exports.hasElems = function hasElems (argObject, argArray)
{
var tempArray = Object.keys(argObject);
if (tempArray.length < argArray.length)
{
return false;
}
else
{
for (var i=0; i < tempArray.length; i++)
{
if ((argArray !=0) && (tempArray[i] != argArray[i]))
{
return false;
}
else
{
return true;
};
}
};
};
vagrant@precise32:/vagrant/frontend-javascript-exercises/10-loops/02-object-operations$ mocha spec.js
copy
✓ should be defined
✓ should create a new object with a copy of all the data
extend
✓ should be defined
✓ should correctly extends two hashes that are unique
✓ should correctly extend two hashes that are have keys in common
hasElems
✓ should be defined
✓ returns true for an empty array
✓ returns true if it has all the keys
✓ returns false if one or more of the keys isn't in the hash
9 passing (13ms)
vagrant@precise32:/vagrant/frontend-javascript-exercises/10-loops/02-object-operations$