Created
October 3, 2018 19:51
-
-
Save ippaiva/a5fc8e822edfc386c9efa11fdf38d507 to your computer and use it in GitHub Desktop.
Arrays-operation created by ippaiva - https://repl.it/@ippaiva/Arrays-operation
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//Getting the last Index | |
var animalArray = ["Dog", "Cat", "Fish"]; | |
// Notice, it's the length - 1 | |
// The array's index starts at 0, and we must compensate | |
var lastIndex = animalArray.length - 1; | |
var lastElement = animalArray[lastIndex]; | |
console.log(lastElement); | |
//Push Method --> add at the end of array | |
var animalArray = ["Dog", "Cat", "Fish"]; | |
animalArray.push("Lizard"); | |
console.log(animalArray); | |
//Adding at a Specific Position in an Array | |
var animalArray = ["Dog", "Cat", "Fish"]; | |
console.log("Original: " + animalArray); | |
animalArray[6] = "Turtle"; | |
console.log("Adding some element: " + animalArray); | |
animalArray[2] = "Snake"; | |
console.log("Updating some element: " + animalArray); | |
//Unshift Merthod -->Add at the beginning of array | |
var animalArray = ["Dog", "Cat", "Fish"]; | |
animalArray.unshift("Whale"); | |
console.log(animalArray); | |
//Slice Method -->defines the index you want to delete, second defines how many items will be deleted from this position. | |
var animalArray = ["Dog", "Cat", "Fish", "Lizard", "Whale", "Cheetah"]; | |
console.log("Original: " + animalArray); | |
console.log("------"); | |
// "From the first element, remove one going forward" | |
animalArray.splice(0, 1); | |
console.log(animalArray); | |
// "From the second element, remove two going forward" | |
animalArray = ["Dog", "Cat", "Fish", "Lizard", "Whale", "Cheetah"]; | |
animalArray.splice(2, 2); | |
console.log(animalArray); | |
// If we pass a third argument | |
// It is inserted as the replacement | |
animalArray = ["Dog", "Cat", "Fish", "Lizard", "Whale", "Cheetah"]; | |
animalArray.splice(0, 1, "Something else"); | |
console.log(animalArray); | |
//Exercise 1: Add two animal at the end of array. | |
animalArray = ["Dog", "Cat", "Fish", "Lizard", "Whale", "Cheetah"]; | |
animalArray.push("bunny","goat"); | |
console.log(animalArray); | |
console.log("------"); | |
//Exercise 2: Remove the last two animals of the end of array. | |
animalArray.splice(0,2); | |
console.log(animalArray); | |
console.log("------"); | |
//Exercise 3 : Replace last animal with the word "Last". | |
animalArray.splice(5,1,"last"); | |
console.log(animalArray); | |
console.log("------"); | |
//For Loop through array | |
var animalArray = ["Dog", "Cat", "Fish"]; | |
for (var i = 0; i < animalArray.length; i++){ | |
console.log(animalArray[i]); | |
} | |
console.log("------"); | |
//while loop through array | |
var counter = 0; | |
var animalArray = ["Dog", "Cat", "Fish"]; | |
while (counter < animalArray.length){ | |
console.log(animalArray[counter]); | |
counter++; | |
} | |
console.log("------"); | |
//forEach is an array method that iterates through each element of the array. | |
var animalArray = ["Dog", "Cat", "Fish"]; | |
animalArray.forEach(function(animal){ | |
console.log(animal); | |
}); | |
console.log("------"); | |
//ForEach Exercise: Create an array with 6 of your favorite foods. | |
//With the loop of your choice | |
//but only print out the foods with an even index. | |
var favFood = ["Oyster","Salmon","Ribs","fried eggs","Chocolate","Salami" ]; | |
for(var i = 0; i < favFood.length; i++){ | |
if(i % 2 === 0) { | |
console.log(favFood[i]); | |
} | |
} | |
console.log("------"); | |
//Sum of Array | |
//Create a variable to hold the total. | |
//Create an array of numbers. | |
//Iterate over each element in the array with a for loop. | |
//In the for loop, add each element of the array to the sum. As you iterate over each number, take the value and add it to the sum. | |
// Print the sum | |
//The final sum should be 29. | |
//Avarage of array | |
//After summed the array, divide the sum by the total number of elements in the array and assign this to a var average | |
//The average should be: 8.1666. | |
var sum = 0; | |
var numbers = [1, 7, 4, 11, 16, 10]; | |
// Add each number in the array to the sum | |
for(var i = 0; i < numbers.length; i++){ | |
sum +=numbers[i]; | |
} | |
console.log("The sum is: " + sum); | |
console.log("The avarage is : " + sum / numbers.length); | |
console.log("------"); | |
//Find the Largest | |
//Create a variable to hold the current largest number called currentLargest. Before you iterate over the array, the value will be null. | |
//Iterate through the array. For each number in the array, compare it to currentLargest. | |
//If the iterated number is greater than currentLargest, it is the new currentLargest. | |
//If it’s less than the currentLargest, nothing happens | |
var currentLargest = null; | |
var smallestNumber = 0; | |
var numbers = [10, 16, 99, 2, 52, 41, 7]; | |
for (var i = 0; i < numbers.length; i++){ | |
if ( numbers[i] > currentLargest ){ | |
currentLargest = numbers[i]; | |
} | |
} | |
console.log("The largest number is: " + currentLargest); | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment