The only issues I can see with your function are that you're doing
myArray;
at the end, which doesn't do anything useful, you're not using the arrayLength variable for anything, and you don't need the myArray variable. The unshift and push are fine.
So perhaps:
var addTwo = function(array) {
array.unshift(1);
array.push(1);
return array;
};
The only reason you need to return array is if the caller doesn't already have a handy reference to it.
Usage examples:
var a = ["apple","orange","banana"];
addTwo(a);
console.log(a); // [1, "apple", "orange", "banana", 1]
var addTwo = function(array) {
array.unshift(1);
array.push(1);
return array;
};
var a = ["apple","orange","banana"];
addTwo(a);
console.log(a); // [1, "apple", "orange", "banana", 1]
and
var a = addTwo(["apple","orange","banana"]);
console.log(a); // [1, "apple", "orange", "banana", 1]
var addTwo = function(array) {
array.unshift(1);
array.push(1);
return array;
};
var a = addTwo(["apple","orange","banana"]);
console.log(a); // [1, "apple", "orange", "banana", 1]
myArray;at the end of the function doesn't do anything. In particular, it doesn't return the value ofmyArray. If you want to return the array reference, doreturn myArray;