Computer Science - Array Quiz

0.0(0)
studied byStudied by 0 people
learnLearn
examPractice Test
spaced repetitionSpaced Repetition
heart puzzleMatch
flashcardsFlashcards
Card Sorting

1/10

flashcard set

Earn XP

Description and Tags

Mr. Rose @Trinity

Study Analytics
Name
Mastery
Learn
Test
Matching
Spaced

No study sessions yet.

11 Terms

1
New cards

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

2
New cards

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

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

3
New cards

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

}

4
New cards

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

}

5
New cards

let myList = [];

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

myList.Push(0);

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

6
New cards

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”

7
New cards

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

console.log(myList[j]

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

8
New cards

let sum = 0;

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

sum+= myList[k];

}

adds all terms

9
New cards

let sum = 0;

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

sum += myList[k]

}

console.log(sum / myList.length)

finds average of terms

10
New cards

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

11
New cards

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