Final answer:
To swap the first and last elements of an array in a function, create a function named SwapArrayEnds() that takes an array as a parameter. Within the function, use a temporary variable to store the first element of the array, assign the last element to the first position, and assign the temporary variable's value to the last position.
Step-by-step explanation:
To swap the first and last elements of an array in a function, you can create a function named SwapArrayEnds() that takes an array as a parameter. Within the function, you can use a temporary variable to store the first element of the array, assign the last element to the first position, and assign the temporary variable's value to the last position. Here is an example:
function SwapArrayEnds(arr) {
let temp = arr[0];
arr[0] = arr[arr.length - 1];
arr[arr.length - 1] = temp;
}
let sortArray = [10, 20, 30, 40];
SwapArrayEnds(sortArray);
console.log(sortArray);
In this example, the function SwapArrayEnds() swaps the first and last elements of the sortArray. After calling the function, the sortArray will be modified to [40, 20, 30, 10].