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?
 
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
 
Ryan ChongCommented:
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
Cloud Class® Course: Ruby Fundamentals

This course will introduce you to Ruby, as well as teach you about classes, methods, variables, data structures, loops, enumerable methods, and finishing touches.

 
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
 
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 ChongCommented:
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
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.