Upload files to "arrays"

This commit is contained in:
2025-10-27 12:26:06 -05:00
parent 3683031350
commit ef28aafda1
5 changed files with 146 additions and 0 deletions

8
arrays/Example1.js Normal file
View File

@@ -0,0 +1,8 @@
// creating an array of strings
let animals = ["Zebra", "Koala", "Lion", "Elephan", "Eagle"]
// displaying the items stored in array animals
console.log(animals)
console.log("\narray contains "+ animals.length +" items")

23
arrays/Example2.js Normal file
View File

@@ -0,0 +1,23 @@
// creating an array of strings
let animals = ["Zebra", "Koala", "Lion", "Elephan", "Eagle"]
// displaying the items stored in array animals using a for loop
for(let i=0; i<animals.length; i++)
{
let item = animals[i] // gets item at location i
console.log('Item ' +i +": " +item)
}
console.log()
// displaying the items using the for-in loop
for(let i in animals)
{
let item = animals[i] // gets item at location i
console.log('Item ' +i +": " +item)
}

59
arrays/Example3.js Normal file
View File

@@ -0,0 +1,59 @@
// METHODS OF ARRAYS
/*
push(elem_list): Adds one or more elements to the end of the array,
and returns the new length of the array.
pop(): removes the last element in the array, decrements the length,
and the element that it removed
unshift(elem_list): adds one or more elements to the beginning of the
array and returns the new length of the array
shift(): Removes the first element in the array, decrements the
array length, and returns the element that it removed
join(seperator): when no parameter is passed, this method converts all the elements
of the array to strings and concatenates them seperated
by commas. To change the seperator, you can pass this method
a string literal
toString(): Same as the join method without any parameter passed to it.
*/
// creating an array of strings
console.log("Original Array")
let animals = ["Zebra", "Koala", "Lion", "Elephan", "Eagle"]
console.log(animals)
console.log()
console.log("Inserting Monkey and Snake at end of array")
animals.push("Monkey", "Snake");
console.log(animals)
console.log()
console.log("Removing last element of array")
animals.pop()
console.log(animals)
console.log()
console.log("Inserting Spider and horse at front of array")
animals.unshift("Spider", "horse")
console.log(animals)
console.log()
console.log("Removing first item from array")
animals.shift()
console.log(animals)
console.log()
console.log("Joining array elements in a single string")
let s = animals.join(" ")
console.log(s)
console.log()
// combining array seaanimals and animals into one array named allanimals
console.log("combining array seaanimals and animals into one array named allanimals")
let seaAnimals = ["Shark", "Whale", "Dolphin"]
let allanimals = seaAnimals.concat(animals)
console.log(allanimals)
console.log()

32
arrays/Example4.js Normal file
View File

@@ -0,0 +1,32 @@
// METHODS OF ARRAYS
/*
push(elem_list): Adds one or more elements to the end of the array,
and returns the new length of the array.
*/
let getSum = function(list)
{
let sum=0
for(let i in list)
{
let n = list[i]
sum = sum + n
}
return sum;
}
let getAverage = function(list)
{
let sum = getSum(list);
let ave = sum/list.length
return ave
}
let numbers = [2, 6, 4, 7, 3, 6]
console.log("Array: " +numbers)
let sum = getSum(numbers)
let average = getAverage(numbers)
console.log("Sum = " +sum )
console.log("Average = " +average)

24
arrays/Example5.js Normal file
View File

@@ -0,0 +1,24 @@
// METHODS OF ARRAYS
/*
push(elem_list): Adds one or more elements to the end of the array,
and returns the new length of the array.
*/
let getSum = function(list)
{
let sum=0
for(let i in list)
{
let n = list[i]
sum = sum + n
}
return sum;
}
let numbers = [2, 6, 4, 7, 3, 6]
let min = Math.min.apply(null, numbers)
let max = Math.max.apply(null, numbers)
console.log("Min = " +min)
console.log("Max = " +max)