Computer Science - Array Quiz

studied byStudied by 0 people
0.0(0)
Get a hint
Hint

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.

1 / 10

flashcard set

Earn XP

Description and Tags

Mr. Rose @Trinity

11 Terms

1

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

New cards
2

Write code to output the length of each of the two arrays.

console.log(evens.length, odds.length);

New cards
3

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

}

New cards
4

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

}

New cards
5

let myList = [];

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

myList.Push(0);

push “0” 9 times in the array “myList”

New cards
6

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”

New cards
7

for(let j = 0; j < myList.length; j++)

console.log(myList[j]

prints out the array until it reaches last index. that’s it.

New cards
8

let sum = 0;

for (let k = 0; k < myList.length; k++){

sum+= myList[k];

}

adds all terms

New cards
9

let sum = 0;

for (let k = 0; k < myList.length; k++){

sum += myList[k]

}

console.log(sum / myList.length)

finds average of terms

New cards
10

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

New cards
11

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

New cards

Explore top notes

note Note
studied byStudied by 29 people
... ago
5.0(2)
note Note
studied byStudied by 7 people
... ago
5.0(1)
note Note
studied byStudied by 599 people
... ago
4.3(7)
note Note
studied byStudied by 37 people
... ago
5.0(2)
note Note
studied byStudied by 11 people
... ago
5.0(2)
note Note
studied byStudied by 14 people
... ago
5.0(1)
note Note
studied byStudied by 20 people
... ago
5.0(2)
note Note
studied byStudied by 3153 people
... ago
4.8(13)

Explore top flashcards

flashcards Flashcard (80)
studied byStudied by 2 people
... ago
5.0(1)
flashcards Flashcard (63)
studied byStudied by 9 people
... ago
5.0(1)
flashcards Flashcard (36)
studied byStudied by 10 people
... ago
5.0(2)
flashcards Flashcard (39)
studied byStudied by 32 people
... ago
5.0(1)
flashcards Flashcard (26)
studied byStudied by 35 people
... ago
5.0(1)
flashcards Flashcard (46)
studied byStudied by 4 people
... ago
5.0(1)
flashcards Flashcard (34)
studied byStudied by 5 people
... ago
5.0(1)
flashcards Flashcard (78)
studied byStudied by 123 people
... ago
5.0(3)
robot