0

I want to write a function named concatArray(array1, array2) that takes two arrays as parameters. This function should return a new array that is a combination of those two.

I tried this, but in the console it shows: 'array1 is not defined'.

function concatArray(array1, array2) {
    var array1 = [1, 3, 7];
    var array2 = [12, 21, 42];
}
console.log(array1.concat(array2));

The expected output should be this: 1,3,7,12,21,42

2
  • You aren't calling concatArray anywhere, so why would array1 be defined? Commented Jun 5, 2019 at 21:49
  • In addition to not calling concatArray, array1 is only valid in the scope of the function. It doesn't exist outside the function concatArray. That's why you're getting the error. It's not really clear what your function is supposed to do. Did you mean to put the console.log inside the function? Commented Jun 5, 2019 at 21:51

4 Answers 4

1

Let me break my answer into a few parts:

  • Why is array1 'undefined'? You are trying to use a variable that is lexically scoped to your function. Check out this post for more. Essentially, by using the keyword var inside of a function, you have declared a variable that can only be accessed from within that function block.

If you were to call console.log from within the function block, it would be defined. Eg:

function concatArray(array1, array2) {
    var array1 = [1, 3, 7];
    var array2 = [12, 21, 42];
    console.log(array1.concat(array2));
}
  • How can I pass two arrays as parameters? Your current implementation does accept two parameters, but you immediately overwrite the values within your function. You can remove those declarations and count on the code that invokes the function to define your two arrays.

Like this:

function concatArray(array1, array2) {
    console.log(array1.concat(array2));
}
  • How do I write a function that concats two arrays? You were really close with what you have. A few things you forgot: 1) a return statement in your function 2) an invocation of your function.

Try something like this:

// define our function
function concatArray(array1, array2) {
    // return two concatted arrays
    return array1.concat(array2);
}

// define our arrays
var myArray1 = [1, 3, 7];
var myArray2 = [12, 21, 42];

//invoke the function with our arrays as params and assign it to a variable
var result = concatArray(myArray1, myArray2);

//console log new results
console.log(result);
Sign up to request clarification or add additional context in comments.

Comments

0

What you need to be doing is done in your console.log statement. But here is the solution

var array1 = [1, 3, 7];
var array2 = [12, 21, 42];

function concatArray(array1, array2) {
  return array1.concat(array2);
}

console.log(concatArray(array1, array2));

Comments

0

Your question is unclear, maybe you're looking for something like that?

function concatArray(arr_1, arr_2)
{
  return [].concat(arr_1, arr_2)  // eq return arr_1.concat(arr_2) 
}

var array1 = [1, 3, 7];
var array2 = [12, 21, 42];
var array3 = concatArray(array1, array2);

console.log('array1',JSON.stringify(array1));
console.log('array2',JSON.stringify(array2));
console.log('array3',JSON.stringify(array3));

Comments

0

You can use the spread operator from ES6.

function concatArray(array1, array2) {
    return [...array1, ...array2];
}

var array1 = [1, 3, 7];
var array2 = [12, 21, 42];
console.log(concatArray(array1, array2));

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.