let evens = [2, 4, 6, 8, 10];
let odds = [];
odds.push(1); odds.push(3); odds.push(5); odds.push(7); odds.push(9); odds.push(11); odds.push(13);
What are both arrays after these declarations? Label the index of each element.
[2, 4, 6, 8, 10]
0 1 2 3 4
[1, 3, 5, 7, 9, 11 ,13]
0 1 2 3 4 5 6
Write code to output the length of each of the two arrays.
console.log(evens.length, odds.length);
Write a for loop to assign a value of 0 to each element in array evens.
for(let i = 0; i < evens.length; i++){
evens[i] = 0
}
Write a for loop to make every other value in array odds negative.
for(let i = 0; i < odds.length; i+=2){
odds[i] *=-1
}
let myList = [];
for (let i = 0; i < 10; i++){
myList.Push(0);
push “0” 9 times in the array “myList”
let myList = [];
for (let z = 0; z < 20; z++)
mylist.push(2*z)
push multiples of 2, starting from 0 up until 38 in the array “myList”
for(let j = 0; j < myList.length; j++)
console.log(myList[j]
prints out the array until it reaches last index. that’s it.
let sum = 0;
for (let k = 0; k < myList.length; k++){
sum+= myList[k];
}
adds all terms
let sum = 0;
for (let k = 0; k < myList.length; k++){
sum += myList[k]
}
console.log(sum / myList.length)
finds average of terms
let sum = 0;
for (let k = 0; k < myList.length; k++){
if (myList[k] % 2 === 0)
sum += myList[k];
}
adds all terms in the array that are multiples of 2
let biggest = myList[0];
let index = 0;
for (let g = 1; g < myList.length; g++){
if (myList[g] > biggest){
index = g;
biggest = myList[g]
}
}
finds biggest number in the array