Array Exercise
Array Exercise
function createArray(n) {
return arr;
Console.log(createArray(5))
function findMax(arr) {
return max;
Console.log(findMax([1,2,6,4,90]))
function findMin(arr) {
min = arr[i];
return min;
Console.log(findMin([3,2,1,7,4]))
4. Write a function to calculate the sum of all elements in an
array.
function sumArray(arr) {
let sum = 0;
sum += arr[i];
return sum;
Console.log(sumArray([1,2,3,4,5,6]))
function averageArray(arr) {
Console.log(averageArray([1,2,3,4,5]))
6. Write a function that counts the number of even and odd
numbers in an array.
function countEvenOdd(arr) {
let evenCount = 0;
let oddCount = 0;
if (arr[i] % 2 === 0) {
evenCount++;
} else {
oddCount++;
Console.log(countEvenOdd(1,2,3,4,5,6,7,8))
7. Write a function to reverse the elements of an array.
function reverseArray(arr) {
reversed.push(arr[i]);
return reversed;
Console.log(reverseArray([1,2,3,4,5]))
function removeDuplicates(arr) {
if (!unique.includes(arr[i])) {
unique.push(arr[i]);
}
return unique;
Console.log(removeDuplicates(1,1,2,3,5,4,7,3,2,5))
return true;
return false;
console.log(containsElement([1,2,3,4,5,6],6))
// Swap
arr[j + 1] = temp;
return arr;
Console.log(bubbleSort([3,4,6,7,1,2]))
let count = 0;
count++;
return count;
Console.log(countElements([1,2,3,4,5,6,7,8,9]))
merged.push(arr1[i]);
merged.push(arr2[j]);
return merged;
}
Console.log(mergedArrays([1,2,3,4],[5,6,7,8]))
return i;
Console.log(findIndex([1,2,3,4],4))
shifted.push(arr[i]);
return shifted;
Console.log(shiftLeft([1,2,3,4]))
function removeLast(arr) {
newArr.push(arr[i]);
return newArr;
Console.log(removeLast([1,2,3,4]))
16.Write a function that retrieves the last element of an array.
function getLastElement(arr) {
Console.log(getLastElement([1,2,3,4,5]))
function getFirstElement(arr) {
return arr[0];
Console.log(getFirstElement([1,2,3,4]))
result.push(arr[i]);
return result;
Console.log(repeatArray([1,2,3,4],3))
subarray.push(arr[i]);
return subarray;
}
Console.log(extractSubarray([1,2,3,4,5],2,4))