Skip to content

Instantly share code, notes, and snippets.

@LukeSkyRunner
Created January 1, 2020 19:45
Show Gist options
  • Save LukeSkyRunner/e54740f1885e4efc3ca053d0c276c0ac to your computer and use it in GitHub Desktop.
Save LukeSkyRunner/e54740f1885e4efc3ca053d0c276c0ac to your computer and use it in GitHub Desktop.
Javascript Array Practice
//Perform a few operations on the given array:
//add two of your favorite animals to the end of the array,
//remove the first two elements of the array,
//replace the last element in the array with the word “last”.
const animalArray = ["dog", "cat", "fish"];
animalArray.push ("lizard","cricket");
console.log (animalArray);
animalArray.splice (0,2);
console.log (animalArray);
animalArray.pop ()
animalArray.push ("last");
console.log (animalArray);
//Create an array with 6 of your favorite foods. With the loop of your choice, iterate through the array, but only print out the foods with an even index.
const favoriteFoods =["salami","sausages","oreo","cola","pizza","sushi"];
for (i=0; i<=favoriteFoods.length; i++){
if (i % 2 === 0) {
console.log (i);
}
}
//Check if the word appears in given array and print out if it does or doesn’t:
let word = "miami";
let arr = ["bcn", "mia", "sao", "mex", "par", "miami", "ams", "ber", "paris", "lis", "mad"];
for (i=0; i<=arr.length; i++){
if (word === arr[i]){
console.log ("Appear");
}else {
console.log ("Not Appear");
}
}
//Given array of numbers, calculate the sum:
const prices = [10.99, 44.56, 112.79, 3, 5];
let sum = 0; // start value for the sum
for (i=0; i<=prices.length; i++){
sum += prices[i];
}
sum.toFixed(2);
console.log (sum);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment