Sample Array
Sample Array
// Ascending order
// Descending order
Ascending Order:
The sort() method is called on the array slice() to create a copy of the original array. This ensures that the
original array remains unchanged.
Inside the sort() method, a compare function (a, b) => a - b is provided. This function is used to
determine the order of elements during sorting.
When a - b is negative, it means that a should come before b in the sorted array.
When a - b is positive, it means that b should come before a in the sorted array.
When a - b is zero, it means that the elements a and b are considered equal in terms of sorting order.
Therefore, using a - b as the comparison, the sort() method arranges elements in ascending order by
placing smaller values before larger ones.
OUTPUT:
let sum = 0;
// Loop through the array and add each element to the sum
sum += array[i];
OUTPUT:
array.push(element);
console.log(array[i]);
OUTPUT:
Element of a[0] = 3
Element of a[1] = 4
Element of a[2] = 5
Element of a[3] = 6
6
// Sample array
// Item to search
foundIndex = i;
} else {
}
// Prompt the user to enter the number of terms
let current = 0;
let previous = 1;
current = previous;
previous = next;
OUTPUT:
Fibonacci series: 0 1 1 2 3