Having issues with array methods

/*
Instructions

Take a look at the myName object. Replace "YOURFIRSTNAME" and "YOURLASTNAME" so it contains your first name and your last name.
We also have provided a makeName function for you that will take a first name and last name and combine them.
Create a variable called firstLast to hold the result of mapping over myName and returning your first and last name using makeName.

Hint
Use dot notation to access the first and last name values.
*/

//GIVEN TASK:

var myName = [{first:"YOURFIRSTNAME", last:"YOURLASTNAME"}]

function makeName(firstName, lastName){
  return firstName + " " + lastName;
}

//code here

Open in new window




// MY SOLUTION IS:

var myName = [{first:"Sean", last:"Thompson"}]

function makeName(firstName, lastName){
  return firstName + " " + lastName;
}

var firstLast = makeName(myName);
console.log(firstLast);

Open in new window




//WHAT IS WRONG HERE?
Temir AlievStudentAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Ryan ChongBusiness Systems Analyst , ex-Senior Application EngineerCommented:
try:

var firstLast = makeName(myName[0].first, myName[0].last);

Open in new window

0
Pawan KumarDatabase ExpertCommented:
Please try tested function.

var myName = [{first:"Sean", last:"Thompson"}]

function makeName(nameObj)
{
  return nameObj.first + " " + nameObj.last;
}

var firstLast = makeName(myName[0]);
document.write(firstLast);

Open in new window


OUTPUT
Sean Thompson

Open in new window

0
Temir AlievStudentAuthor Commented:
Thanks Pawan Kumar,
Your code worked perfectly fine, but the school platform where I am being tested on is not accepting this solution since it is not following the instructions defined above in the description. Ryan's version i think met all the requirements and yours definitely solves the problem but I guess it is a bug on the platform which is why it is not accepting the answer.
0
Big Business Goals? Which KPIs Will Help You

The most successful MSPs rely on metrics – known as key performance indicators (KPIs) – for making informed decisions that help their businesses thrive, rather than just survive. This eBook provides an overview of the most important KPIs used by top MSPs.

Julian HansenCommented:
I think the key word here is mapping - you have an array of objects and you want to map over those objects
<script>
var myName = [{first:"Sean", last:"Thompson"}]
	
function makeName(firstName, lastName){
  return firstName + " " + lastName;
}

var firstLast = myName.map(function(item) {
	return makeName(item.first, item.last);
});

console.log(firstLast);
</script>

Open in new window

Output
Array [ "Sean Thompson" ]

Open in new window

What they are trying to teach you here is how to iterate over an array using map() to create a new array of results from the first object.
The javascript map() function is used to iterate over an array and return a new array item for each array item found in the original array.

Your original array is an array of objects containing first name / last name  - what I think they want is a result array of combined name - and we do that with the map.

To illustrate how this would work with multiple objects consider this sample data using the same code above
var myName = [{first:"Sean", last:"Thompson"}, {first:"Jack", last: "Frost"}, {first: "John", last: "Smith"}];

Open in new window

The output from this would now be
Array [ "Sean Thompson", "Jack Frost", "John Smith" ]

Open in new window

Can you see how we have taken an array of one type of thing and morphed it into a new array of new things each derived from the data from the first array?

The map() function is used extensively in JavaScript applications - it is well worth putting the time in to understand what it does and how we use it.
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Julian HansenCommented:
I disagree with the allocation. The author already stated that the proposed solution
https://www.experts-exchange.com/questions/29070127/Having-issues-with-array-methods.html#a42380104
and
https://www.experts-exchange.com/questions/29070127/Having-issues-with-array-methods.html#a42380067
Did not pass muster on the school platform.

This is because they were not correct in terms of the requirement of the teaching example. The reason being that the teaching example was teaching how to use mapping of arrays - which neither of the above solutions dealt with.

This solution https://www.experts-exchange.com/questions/29070127/Having-issues-with-array-methods.html#a42381392 is the only one that deals with the issue of mapping.

While the other two samples produced the correct output - that was not the point of the teaching exercise. In a teaching example you get the solution correct not only when you achieve the right answer but also the right method - the right method here was to use the map function.
0
Ryan ChongBusiness Systems Analyst , ex-Senior Application EngineerCommented:
Thanks Pawan Kumar,
Your code worked perfectly fine, but the school platform where I am being tested on is not accepting this solution since it is not following the instructions defined above in the description. Ryan's version i think met all the requirements and yours definitely solves the problem but I guess it is a bug on the platform which is why it is not accepting the answer.

We need the response from asker, if it's not, then I would suggest to delete this question.
1
Temir AlievStudentAuthor Commented:
Sorry I totally forgot to close this question, Julians answer was accepted by the  platform, I dont really understand how this awesome website's point system works, seems like there is a discussion over whose solution was most helpful. You guys all helped me understand the concept even if not all the provided solutions were accepted, at least now I know why certain solutions work in different cases. Thank you all, I am extremely sorry for not ,closing it in a timely manner.
0
Pawan KumarDatabase ExpertCommented:
Dear Temir,

Going forward please mention about your testing platform in the requirement itself.
1
Julian HansenCommented:
@Pawan,
Please refer the author's comment
Thanks Pawan Kumar,
Your code worked perfectly fine, but the school platform where I am being tested on is not accepting this
0
Pawan KumarDatabase ExpertCommented:
@Julian,
This we got after submitting the comment. This was not mentioned in the original question.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
arrays

From novice to tech pro — start learning today.