I am trying to solve this challenge.
Question
Given are two arrays a and b, both have three elements. Return a new array of length 2 containing both middle (index 1) elements of the arrays).
Error
Uncaught ReferenceError: a is not defined on line: 10 First Try
Second Try
First try
function goldenMiddle(a, b) {
a = [4,6,2]; // assign an array to a and b
a = [1,6,8];
var c = []; // created a new array
var resultA = a[1]; // index 1 of array a
var resultB = a[1]; // index 1 of array b
var newResult = c.push(resultA) + c.push(resultB); // push the resultA and resultB to c
return c; // return c
}
goldenMiddle()
Second Try
function goldenMiddle(a, b) {
a = [];
a = [];
var c = []; // created a new array
var resultA = a[1]; // index 1 of array a
var resultB = a[1]; // index 1 of array b
var newResult = c.push(resultA) + c.push(resultB); // push the resultA and resultB to c
return c; // return c
}
goldenMiddle(a[1,6,8], b[4,6,2])
Any idea what the problem might be?
Thanks
2
Answers
For your error, you must call goldenMiddle like this
However, to accomplish your function, there are a couple other things you need to adjust
In both tries, you’re only using the parameter
a
and notb
.By using
a = []
, you are clearing the array.Also, Arrays are directly passed as parameters simply just using
[]
.Your code can be simply like this: