0% found this document useful (0 votes)
44 views3 pages

CSCE 120: Learning To Code: Processing Data I Hacktivity 8.1

This document provides instructions for Hacktivity 8.1 of the CSCE 120: Learning To Code course. Students will be randomly assigned partners to work through the hacktivity together using peer programming. One partner will be the driver, controlling the keyboard and computer, while the other is the navigator, directing the activity. The hacktivity involves completing knowledge checks about loops and arrays, warm-up exercises to practice loops and arrays, and implementing a number guessing game in JavaScript.

Uploaded by

s_gamal15
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
44 views3 pages

CSCE 120: Learning To Code: Processing Data I Hacktivity 8.1

This document provides instructions for Hacktivity 8.1 of the CSCE 120: Learning To Code course. Students will be randomly assigned partners to work through the hacktivity together using peer programming. One partner will be the driver, controlling the keyboard and computer, while the other is the navigator, directing the activity. The hacktivity involves completing knowledge checks about loops and arrays, warm-up exercises to practice loops and arrays, and implementing a number guessing game in JavaScript.

Uploaded by

s_gamal15
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 3

CSCE 120: Learning To Code

Processing Data I
Hacktivity 8.1

Introduction
Prior to engaging in this hacktivity, you should have completed all of the pre-class ac-
tivities as outlined in this module. At the start of class, you will be randomly assigned
a partner to work with on the entirety of this hacktivity as a peer programming activity.
Your instructor will inform you of your partner for this class.
One of you will be the driver and the other will take on the navigator role. Recall that a
driver is in charge of the keyboard and computer while the navigator is in charge of the
handout and directing the activity. However, you are both responsible for contributing
and discussing solutions. If you were a driver/navigator in the prior activity, switch roles
for this activity.

1 Knowledge Check
With your partner, discuss and answer each of the following questions. Write your answers
down on a separate sheet of paper or type them up in a plain text file.

1. What are the three essential components of a loop control structure?


2. Describe what each of the following does to the variable i :
a) i++;
b) i--;
c) i += 10;
d) i -= 3;
3. For each of the following code snippets, indicate what they print to the console.

1
1 for(var i=1; i<=10; i++) {
a) 2 console.log(i);
3 }
1 for(var i=17; i>0; i-=3) {
b) 2 console.log(i);
3 }
1 var i = 10;
2 while(i >= 0) {
c) 3 console.log(i);
4 i--;
5 }
1 var i = 3;
2 while(i < 11) {
d) 3 console.log(i);
4 i += 5;
5 }
1 for(var i=1; i<=5; i++) {
2 for(var j=1; j<=5; j++) {
e) 3 console.log( i * j );
4 }
5 }
4. Consider the following array of elements:
var arr = [8, 4, 2, 9, 3, 1, 5];
For each of the following code snippets, indicate what they print to the console or
what effect(s) they have.
1 arr.forEach(function(element, index) {
a) 2 console.log(element);
3 }
1 arr.forEach(function(element, index, a) {
b) 2 a[index] = element + 10;
3 }
1 $.each(arr, function(index, value) {
c) 2 console.log(index + value);
3 });

2 Warm-up Exercises
Download the code weve provided from GitHub using the URL, https://github.com/
cbourke/LoopProject. Open the project in Light Table and open the file, exercises/exercises.js .

2
Complete the following exercises.

1. Rewrite the for loop in Question 3(a) above as an equivalent while loop. Then
rewrite the while loop in Question 3(c) above as an equivalent for loop.
2. Write a loop that sums integers 1+2+3+ +100 and prints the answer. Generalize
your loop so that it works for any sum 1 + 2 + + n
3. Write a loop that iterates over elements in an array and adds one to each value
4. A common interview question for programming is the FizzBuzz question. The task
is to print out integers 1 through 100. However, for any number that is divisible
by 3, output Fizz and for any number that is divisible by 5, output Buzz . For
any number that is divisible by 3 and 5, output FizzBuzz . Solve the FizzBuzz
problem using JavaScript. Recall that you can test if a number is divisible by
another number using the mod operator: (x % 2) === 0 for example is true if x
is even.
5. Suppose we have an array of integers and an integer n. Write a snippet of code to
determine all the integers 1 through n that are missing from the array. For example,
if our array is var arr = [8, 2, 9, 4, 3, 3, 4] and n = 10, then we should
output 1, 5, 6, 7, 10.
6. Would you take a job if I paid you a dollar per week, but promised to double your
pay each week? Figure out how much you would earn in your first six months (26
weeks) if you said yes by writing code to compute it.

3 Number Guessing Game


The Number Guessing Game is a game where one player (the computer) thinks of a
number x between 1 and 100. Then the other player (the user) makes a guess. If the
guess is correct, the game is over. Otherwise, the computer informs the user whether or
not the number x is higher or lower than their guess as a hint. The player continues to
guess until they guess correctly.
Implement this game in JavaScript. To do so, weve included a starter file, exercises/guess.js
that generates a random integer between 1 and 100 for you. Use prompt() to read in
guesses from the user and alert() to inform them of their win/number of guesses.

You might also like