-1

i have a function in javascript where i need to retrieve the last element of the list. the list can be an array, string, list in numbers (not array). I tried converting the list into a String and then an array and retrieving it by index, but that's not working.

Here is the code I tried:

  function last(list){
      var array = new String(list);
      array = array.split("");
      return array[array.length-1];
  }

I don't understand what the problem is because test suite says Expected: 5 instead got: 5 I am using code wars and did not write the tests. Is it expecting a Number and getting a String '5' ? I don't understand types in loosely typed languages very well yet.

user137717
  • 2,005
  • 4
  • 25
  • 48
  • `list in numbers (not array)` - What would that be? – thefourtheye Oct 18 '14 at 05:22
  • could you post the test? – Nicolas Straub Oct 18 '14 at 05:23
  • @thefourtheye this is one of the tests Test.assertEquals(last(1,"b",3,"d",5), 5);//-- arguments – user137717 Oct 18 '14 at 05:29
  • @NicolásStraubValdivieso Here are the tests that are visible to me and it stops running after failing a test so it stops at the first one. Test.assertEquals(last([1,2,3,4,5]), 5); //-- array Test.assertEquals(last("abcde"), "e"); //-- string Test.assertEquals(last(1,"b",3,"d",5), 5);//-- arguments – user137717 Oct 18 '14 at 05:30
  • `Is it expecting a Number and getting a String '5' ?` Very possible! – Shomz Oct 18 '14 at 05:33
  • @user137717 for `Test.assertEquals(last([1,2,3,4,5]), 5);`, `Test.assertEquals` probably tests with non coercive equality (`===`), so you need to cast it. That said, your other comment makes me think your code isn't the proper solution to the issue. I'm coding an answer now – Nicolas Straub Oct 18 '14 at 05:34

1 Answers1

0

from the comments, I think you mean you want to either return the last element in an array, the last character in a string, or the last argument passed if multiple arguments were passed. This would do it:

function last() {
    if (arguments.length > 1) { // first we handle the case of multiple arguments
        return Array.prototype.pop.call(arguments);
    }
    value = arguments[0]
    if (typeof value === 'string') { // next, let's handle strings
        return value.split('').pop();
    }

    if (Object.prototype.toString.call( [] ) === '[object Array]') {// Arrays are of type object in js, so we need to do a weird check
        return value.pop();
    }
}

arguments is a pseudo-array that contains all arguments passed into the function, so for last(1,2,3,4,5), arguments would be roughly [1,2,3,4,5]. It's not exactly that though, because arguments has all args in order and a length property, but it's prototype isn't Array, so it isn't truly [1,2,3,4,5] and lacks all of array's functions. This is why we need to call pop in the context of arguments (in javascript, Function.prototype.call calls the function passing the first arguments as the value of this, and all the rest of the arguments into the arguments pseudo-array, so for example last.call([], 1, 2, 3) would call last in the context of a new array and with arguments roughly equal to [1,2,3]).

the rest of the code is pretty straightforward, except for the check to see if value is an array, which is further explained here.

Finally, pop is an array method that removes the last element from an array and returns it.

Community
  • 1
  • 1
Nicolas Straub
  • 3,381
  • 6
  • 21
  • 42