write function 'transformfirstandlast' takes in array, , returns object with: 1) first element of array object's key, , 2) last element of array key's value.
example input: ['queen', 'elizabeth', 'of hearts', 'beyonce'],
function's return value (output): { queen : 'beyonce' } note input array may have varying number of elements. code should flexibly accommodate that.
e.g. should handle input like: ['kevin', 'bacon', 'love', 'hart', 'costner', 'spacey']
here code wrote:
function transformfirstandlast(array) { var arraylist = ['queen', 'elizabeth', 'of hearts', 'beyonce']; var arraylist2 = ['kevin', 'bacon', 'love', 'hart', 'costner', 'spacey'] var myobject = {}; key = arraylist.shift(); value = arraylist.pop(); myobject[key] = value var myobj2 = {}; key = arraylist2.shift(); value = arraylist2.pop(); myobj2[key] = value; console.log(myobject); console.log(myobj2); } transformfirstandlast();
the output this:
{ queen: 'beyonce' } { kevin: 'spacey' } => undefined
i know supposed have return statement, not use 2 arrays 2 objects within 1 function, anyways, not know if doing totally wrong or pretty close find solution, please explain why getting "undefined" message on third line on output screen? thanks!
just used array indexes value , create objects.
function transformfirstandlast(array) { var myobject = {}; myobject[array[0]] = array[array.length-1]; return myobject; } var arraylist = ['queen', 'elizabeth', 'of hearts', 'beyonce']; var arraylist2 = ['kevin', 'bacon', 'love', 'hart', 'costner', 'spacey'] console.log(transformfirstandlast(arraylist)); console.log(transformfirstandlast(arraylist2));
Comments
Post a Comment