Arrow functions, multi arg,?

Tell us what’s happening:
Describe your issue in detail here.

  **Your code so far**
const myConcat = (arr1, arr2) => (arr1.concat(arr2));
myConcat(arr1.concat(arr2)) 

console.log(myConcat([1, 2], [3, 4, 5]));

I am told arr1 is not defined. But arr2 is ??
To do;

Rewrite the myConcat function which appends contents of arr2 to arr1 so that the function uses arrow function syntax.
var myConcat = function(arr1, arr2) {

return arr1.concat(arr2);

};

console.log(myConcat([1, 2], [3, 4, 5]));

  **Your browser information:**

User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/91.0.4472.124 Safari/537.36

Challenge: Write Arrow Functions with Parameters

Link to the challenge:

The line:

myConcat(arr1.concat(arr2)) 

won’t work because arr1 the variable only exists inside the myConcat function - it is not defined outside of that scope.

Does that make sense?

2 Likes

When I remove that line, your code passes for me.

Thnks for yr reply.
no. pls explain more. No outer parenthes? thnks .

I’m not sure that I understand what you are meaning by “outer parentheses”. You asked about an error and I explained what caused that. Parentheses had nothing to do with it.

Oh wait, do you mean this:

const myConcat = (arr1, arr2) => (arr1.concat(arr2));

That should work exactly the same as:

const myConcat = (arr1, arr2) => arr1.concat(arr2);

The issue with your code wasn’t that, it was this:

myConcat(arr1.concat(arr2)) 

It was trying to use a variable out of scope. “arr1” is only defined inside the function definition. This is outside the function definition.

If this does not answer your question, please boil this down to the code in question, say exactly what you think it should do, and exactly what you think it should do instead.

Being able to describe and explain code problems is an important skill for a developer.

1 Like

No, arr2 is also undefined, but the script stops when it gets an error and you get no more output about following errors.

I’m going to break down your code line by line:

  1. You define a function that takes two arguments:
const myConcat = (arr1, arr2) => (arr1.concat(arr2));
  1. You call that function but you provide only ONE argument
myConcat(arr1.concat(arr2)) 

At this point, JavaScript has no idea what arr1 and arr2 are. You’ve never defined them anywhere, causing the undefined error.

  1. Now you call the function again within a console.log, and this time you provide two arrays, so the myConcat function knows what’s arr1 and arr2, and returns the concatinated array:
console.log(myConcat([1, 2], [3, 4, 5]));
2 Likes

Thnks again KS !!, yes i tried both. With and without the (). the code part with myConcat(arr1.concat(arr2))
is just not to be added. and i must repeat the scope lessons again !!.

TC.

Scope is a confusing concept when you are starting out, and JS has some oddities. Don’t worry, you’ll get there. Just keep working, learning, and asking questions, like you’re doing.

Thnks again !! Cool. :+1:

Goodmusic!! thks. !! very helpful.

This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.