Given an object and a key, I created a function that returns an array containing all the elements of the array located at the given key that are equal to 10.
If the array is empty, it should return an empty array and if the array contains no elements equal to 10, it should return an empty array and If the property at the given key is not an array, it should return an empty array.
function getElementsThatEqual10AtProperty(obj, key) {
for(var prop in obj){
if(obj[prop] === 10){
return obj[key];
}
}
}
var obj = {
key: [1000, 10, 50, 10]
};
var output = getElementsThatEqual10AtProperty(obj, 'key');
console.log(output); // --> IT MUST RETURN [10, 10]
Any idea what am I doing wrong?