1 3

advertisement
SI110 Homework Alpha: ________ Name: ____________________
Page 1 of 3
Collaboration Policy: Default
MIDN Last, F.
choose one: □ None □ XS110 □ EI with:
(or more)
□ MGSP
□ Discussed with: ____________________________
Homework: /SI110/Cyber Battlefield/Programs – Conditionals & Loops
1. Consider the following code.
Hint: You can copy and paste the code into the batch interpreter to test!
var a = prompt("Enter a:");
if (a < 17) {
if (a <= 2) {
alert("goat");
} else {
alert('cat');
}
} else {
alert('goat');
}
a. [10 / 8 / 5 / 0] What is the output when the user enters (input):
-1
2
4
Input:
16
19
Output:
b. [5 / 0] The program outputs cat if (circle one):
( a > 2 ) || ( a < 17 )
( a > 2 ) && ( a < 17 )
2. The link below pulls up a web page that acts like a basic ATM. There is a simple JavaScript
program that carries out an ATM transaction when the user clicks the “make transaction” button,
allowing you (the customer) to withdraw money from your bank account. The JavaScript code is
displayed on the webpage for your convenience. You are not the programmer, you are the user!
Find some input that breaks this program; i.e. that makes it behave unlike a normal ATM that a
bank would want!
http://rona.academy.usna.edu/~si110/lec/prgmLoops/hw/bank.html
a. [ 5 / 3 / 0] What input did you enter to break the program?
b. [ 5 / 3 / 0] What incorrect program behavior resulted from your input?
c. [ 5 / 3 / 0 ] Now, if you are the programmer, how would you change the code to fix this
problem? You can test ideas with:
http://rona.academy.usna.edu/~si110/lec/prgmLoops/hw/bankHW.html
SI110 Homework
Collaboration Policy: Default
Page
2 of 3
3. The URL below pulls up the following program in the batch interpreter. Experiment with it a
bit. http://rona.academy.usna.edu/~si110/lec/prgmLoops/hw/number.html
var input1 = prompt("Enter a number between 0 and 10.");
var n = Number(input1);
while( input1 == "" || isNaN(n) || n < 0 || n > 10 ) {
input1 = prompt("Try again! Enter a number between 0 and 10.");
n = Number(input1);
}
alert("You entered the number: " + n + ", like we asked.");
a. [ 5 / 3 / 0] Suppose the user enters USMA and presses the OK button. Circle and mark with
an A the condition that detects that this input is bad.
b. [ 5 / 3 / 0] Suppose the user enters nothing and simply presses the OK button. Circle and
mark with a B the condition that detects that this input is bad.
c. [ 5 / 3 / 0] What can you as a user can do to trick this program into thinking you entered a
number between 0 and 10 without actually having typed in a number between 0 and 10? This
may take some trial-and-error and playing around with the program! DO NOT alter the source
code to solve this problem.
4. [ 20 / 15 / 10 / 0 ] The following program simulates 10 rolls of a 6-sided die, and prints out the
total sum of the 10 rolls. Annotate the code to show how to change the program so that the user
must specify the number of rolls to simulate. Test your changes in the batch interpreter.
var rollsMade = 0;
var total = 0;
while ( rollsMade < 10 ) {
var roll = Math.ceil( 6 * Math.random() );
total = total + roll;
rollsMade = rollsMade + 1;
}
alert("The sum of your 10 dice rolls is " + total);
SI110 Homework
Collaboration Policy: Default
Page
3 of 3
5. The below URLs are to two different versions of a website that uses JavaScript to protect a
really important secret with a pin number. For each version a snippet of the JavaScript code that
gets the pin from the user and checks it is shown below. The only difference between them is the
use of eval( ).
http://rona.academy.usna.edu/~si110/lec/prgmLoops/hw/sensitive1.html
// Sensitive 1
var tmp = prompt("Enter code:");
if ( tmp == spin ) {
// CODE THAT SHOWS THE SECRET
} ...
http://rona.academy.usna.edu/~si110/lec/prgmLoops/hw/sensitive2.html
// Sensitive 2
var tmp = eval(prompt("Enter code:"));
if ( tmp == spin ) {
// CODE THAT SHOWS THE SECRET
} ...
a. [10 / 8 / 5 / 0] What is the secret?
b. [15 / 10 / 5 / 0] Discuss which version you tricked to find the secret, and how you tricked
the program. Use complete sentences, spelling and grammar count.
c. [ 10 / 8 / 5 / 0 ] What is the code? Note: this is a bit trickier
Download