Create a function called 'transformFirstAndLast' which takes an array as input and outputs an object with:
- The first element of the array as the key of the object.
- The last element of the array as the value of that key.
For example, if the input is:
['Queen', 'Elizabeth', 'Of Hearts', 'Beyonce']
The return value of the function should be:
{
Queen: 'Beyonce'
}
Your code should be able to handle arrays of varying lengths. For instance:
['Kevin', 'Bacon', 'Love', 'Hart', 'Costner', 'Spacey']
Here is your existing code:
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 you are getting:
{ Queen: 'Beyonce' }
{ Kevin: 'Spacey' }
=> undefined
You mentioned the issue with the "undefined" message - this might be due to missing a return statement in your function. Additionally, it seems like you are using two predefined arrays instead of utilizing the function parameter 'array'. Try modifying your code to properly accept 'array' as a parameter and generate the desired output accordingly.