arrays - get element from arbitrary list in javascript -
I have a function in javascript where I need to retrieve the last element of the list. I list an array, string, Numbers can be list (not array). I tried to convert the list to string and then tried to retrieve an array and index it by index, but it is not working.
Here I have tried the code:
function Previous list) {var array = New string (list); Array = array.split (""); Return array [array.length-1]; }
I do not understand what the problem is because the trial suite estimates: 5 instead of 5: 5 I am using code warfare and not writing a test I am Does it expect a number and is getting the string '5'? I do not understand very well in loose typed languages.
From the comments, I think that means you want to return the last element in either array The last letter in a string, or if the last argument is passed, it will:
function last () {if (arguments.length & gt; 1) {// most First we handle the case of multiple arguments: Array.prototype.pop.call (logic); } Value = arguments [0] If (typeof value === 'string') {// Next, we handle string return value.split (''). Pop (); } If (Object.prototype.toString.call ([]) === '[Object Array]') {// array is of type object in JS, we have to do a weird check return value.pop (); }}
The rest of the code is very straightforward, except to see that the Finally, pop is an array method that removes the last element from an array and gives it back.
Arguments
is a pseudo-array in which all the arguments are in the function, hence the the last ( 1,2,3,4, 5)
, logic
would be roughly [1,2,3,4,5]
. It is not at all that though, because there are all the arguments in the order and length of the logic
, but this is not the prototype array
, so it is actually There is no [1,2,3,4,5]
and lack of all array functions, that is the reason why we have to understand pop
in the context of arguments
Need to call (in javascript, Function.prototype.call
debate
as the value of this in a pseudo-array, and the rest For arguments, for example last.call ([], 1, 2, 3)
last
in the context of a new array and arguments
will call [1,2,3]
). value
is an array which is explained further.
Comments
Post a Comment