0

Here is the problem:

Given an object and a key, write a function that returns an array containing all but the last element of the array located at the given key.

Notes:

  • If the array is empty, it should return an empty array.
  • If the property at the given key is not an array, it return an empty array.
  • If there is no property at the key, it should return an empty array.

My Code:

var obj = {
key: [1, 2, 3]
};

function getAllButLastElementOfProperty(obj, key) {
var arr = []
arr = obj.key.pop()
return obj.key
}

getAllButLastElementOfProperty(obj, 'key') // [1, 2]

Question:

Why does it fail every single repl test including incuding "should return an array containing all but the last element of the array located at key"??

Vadim Kotov
  • 8,084
  • 8
  • 48
  • 62

4 Answers4

0

Your function is looking for key property every time you run it regardless of what you enter for the key parameter in function getAllButLastElementOfProperty(obj, key).

Try modifying it like so:

var test_obj = {
    key: [1, 2, 3],
    banana: [3,2,1],
};

function getAllButLastElementOfProperty(obj, key) {
    var new_obj = obj;
    new_obj[key].pop()
    return new_obj[key];
}

console.log(getAllButLastElementOfProperty(test_obj, 'banana')) // [1, 2]

That will utilize the key parameter from your function as the property of obj.

EDIT: I would actually use the following function so that it didn't modify the original object (test_obj):

function getAllButLastElementOfProperty(obj, key) {
    var arr = obj[key].slice(0); // Duplicate array without modifying original in test_obj
    arr.pop();  // remove last element
    return arr;
}
Ding
  • 3,065
  • 1
  • 16
  • 27
0

This is what you could do, verify the value at key is Array or not, and then proceed to remove the last element from the array. Hope this helps.

var obj = {
  key: [1, 2, 3],
  newKey: [4, 5],
  singleValue: [1],
  emptyArray: [],
  objectKey: {}
};

function getAllButLastElementOfProperty(obj, key) {
  var arr = [],
    objectAtKey = obj[key];
  if (Array.isArray(objectAtKey)) {
    arr = objectAtKey.slice(0, objectAtKey.length - 1);
  }
  return arr;
}

console.log(getAllButLastElementOfProperty(obj, 'key'));
console.log(getAllButLastElementOfProperty(obj, 'newKey'));
console.log(getAllButLastElementOfProperty(obj, 'singleValue'));
console.log(getAllButLastElementOfProperty(obj, 'emptyArray'));
console.log(getAllButLastElementOfProperty(obj, 'objectArr'));
console.log(getAllButLastElementOfProperty(obj, 'nonExistentKey'));
Sreekanth
  • 3,110
  • 10
  • 22
  • Thanks. This is the only one that passes all the tests. The top one returns all but the last but does not pass the repl test that it returns an array with all elements but last – sopstem2428 Oct 31 '16 at 05:48
  • did the solution I have put here, worked for all of your cases? I dont understand "The top one returns all but the last but does not pass the repl test that it returns an array with all elements but last " Is there something that doesn't work / missing in my code snippet. – Sreekanth Oct 31 '16 at 05:55
  • It returns the correct value to me but when I press submit in repl it says it doesn't "return an array with all elements but the last. It satisfies the notes just fine. check out my new question. It returns every value I need and satisfies all the notes but says it doesn't pass any of them. I am VERY confused – sopstem2428 Oct 31 '16 at 15:56
0

There is a usage of pop(). This method deletes the last value of the used array. Everytime you run this method it shortens the array by one. Therefore use Array.prototype.slice(). It doesn't change the array.

Test with Array.isArray() if the item is an array:

if(!Array.isArray(item)) { return []; }

In total

var obj = {
  key: [1, 2, 3]
};

function getAllButLastElementOfProperty(obj, key) {
  if (!Array.isArray(obj.key)) {
    return [];
  }
  var res = [],
    arr = obj.key;
  res = arr.slice(0, (arr.length - 1)); // slice() doesn't change the used array
  return res;
}

console.log(getAllButLastElementOfProperty(obj, "key"));
console.log(getAllButLastElementOfProperty(obj, "key"));
console.log(getAllButLastElementOfProperty(obj, "key"));
console.log(getAllButLastElementOfProperty(obj, "key"));
0
var obj = {
   key: [1, 2, 3]
};

function getAllButLastElementOfProperty(obj, key) {
    if(obj.key.length < 1 || !Array.isArray(obj.key) || !obj.hasOwnProperty(key)) { 
        return []; 
    }
    var newObj = obj.key.slice(0, obj.key.length - 1);
    return newObj;
}

var output = getAllButLastElementOfProperty(obj, 'key');
output;

I had the same issues...this runs correctly but for some reason I'm getting the same failed test: "TypeError: Cannot read property 'length' of undefined" for all the tests. I will try the most recent solution, but can some tell me why this is throwing that error? thanks

iamwill
  • 1
  • 2