Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
27 views

Sample Array

Uploaded by

Kio Kiks
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
27 views

Sample Array

Uploaded by

Kio Kiks
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 6

// Sample array

const array = [23, 5, 67, 20, 3, 30, 79, 3, 70, 2];

// Ascending order

const ascendingOrder = array.slice().sort((a, b) => a - b);

// Descending order

const descendingOrder = array.slice().sort((a, b) => b - a);

// Displaying the results

console.log("Array =", array);

console.log("Ascending Order =", ascendingOrder);

console.log("Descending Order =", descendingOrder);

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.

In the compare function:

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:

Array = [ 23, 5, 67, 20, 3, 30, 79, 3, 70, 2]

Ascending Order = [ 2, 3, 3, 5, 20, 23, 30, 67, 70, 79]

Descending Order = [ 79, 70, 67, 30, 23, 20, 5, 3, 3, 2]


// Sample array

const array = [10, 20, 30, 40, 50, 60];

// Initialize sum variable

let sum = 0;

// Loop through the array and add each element to the sum

for (let i = 0; i < array.length; i++) {

sum += array[i];

// Displaying the result

console.log("Array =", array);

console.log("Sum of Array Elements =", sum);

OUTPUT:

Array = [ 10, 20, 30, 40, 50, 60 ]

Sum of Array Elements = 210


// Prompting the user to enter the array limit

const limit = parseInt(prompt("Enter the Array Limit:"));

// Initializing an empty array

const array = [];

// Taking input for each element of the array

for (let i = 0; i < limit; i++) {

const element = parseInt(prompt(`Element of a[`+ i +'] = '));

array.push(element);

// Displaying the array elements

console.log("\nDisplay Array Elements:");

for (let i = 0; i < limit; i++) {

console.log(array[i]);

OUTPUT:

Enter the Array Limit:4

Element of a[0] = 3

Element of a[1] = 4

Element of a[2] = 5

Element of a[3] = 6

Display Array Elements:

6
// Sample array

const array = [10, 20, 30, 56, 84];

// Item to search

const itemToSearch = 30;

// Initialize a variable to store the index of the found item

let foundIndex = -1;

// Linear search algorithm

for (let i = 0; i < array.length; i++) {

if (array[i] === itemToSearch) {

foundIndex = i;

break; // Exit the loop once the item is found

// Displaying the result

console.log("Array =", array);

console.log("Item to Search =", itemToSearch);

if (foundIndex !== -1) {

console.log("Item Found at Index =", foundIndex);

} else {

console.log("Item not found in the array.");

}
// Prompt the user to enter the number of terms

const terms = parseInt(prompt("Enter the number of terms in the sequence:"));

// Initialize variables to store the current and previous Fibonacci numbers

let current = 0;

let previous = 1;

// Initialize a variable to store the Fibonacci series as a string

let fibonacciSeries = '';

// Iterate to generate the Fibonacci series

for (let i = 1; i <= terms; i++) {

fibonacciSeries += current + ' ';

const next = current + previous;

current = previous;

previous = next;

// Output the Fibonacci series

console.log(`Fibonacci series: ${fibonacciSeries}`);

OUTPUT:

Enter the number of terms in the sequence:5

Fibonacci series: 0 1 1 2 3

You might also like