SI110 Spring AY13 Alpha:___________ Name:________________________________

advertisement
SI110 Spring AY13
choose one:
(or more)
Alpha:___________
□ Received
no help
Name:________________________________ Page 1 of 2
□ Received help from:
□ Collaborated with:________________________________________
Homework:
/SI110/The Cyber Battlefield/Programs 3
1 & 2. Go to http://rona.cs.usna.edu/~si110/lec/l07/hw/boolop.html and work on the two
problems (shown below). You enter a condition and press the “test” button to see if it
passes all our tests. Make sure it does, then write your solution into the box below.
You may assume for these problems that the user enters a number!
Check one: □ I didn't test
□ I did test on
and it didn't
□ I did test on
and it worked
on webpage
webpage
work right
webpage
20 / 16 / 12 / 0
right
3. Consider the following code.
var a = prompt("enter a");
if (a > 5)
{
if (a < 10)
{
alert('dog');
}
else
{
alert('cat');
}
}
else
{
alert('cat');
}
Check one: □ I didn't test
□ I did test on
and it didn't
□ I did test on
and it worked
on webpage
webpage
work right
webpage
20 / 16 / 12 / 0
right
a. What is output when the user enters:
i.
7
________
3/0/0/0
ii.
14
________
3/0/0/0
iii.
-7
________
3/0/0/0
iv.
5
________
3/0/0/0
v.
10
________
3/0/0/0
b. The program outputs dog if (circle one)
a > 5 || a < 10
a > 5 && a < 10
10 / 0/ 0 / 0
SI110 Spring AY13
Alpha:___________
Name:________________________________ Page 2 of 2
4. The link below pulls up a webpage 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. Find
some input that breaks this program! I.e. that makes it behave incorrectly.
http://rona.cs.usna.edu/~si110/lec/l07/hw/bank.html
a. What input did you enter to break the program?
b. What incorrect program behavior resulted from that input?
10 / 8 / 6 / 0
10 / 8 / 6 / 0
15 / 12 / 8 / 0
c. Show how to change the code to fix this problem?
You can test ideas with: http://rona.cs.usna.edu/~si110/lec/l07/hw/bankHW.html
// Get input from user
var input = prompt("Your balance is $" + balance
+ ". How much would you like to withdraw?");
var request = Number(input);
var withdrawn = 0;
//
//
//
if
{
Only make the withdrawl if user inputs a valid number and
the account has sufficient funds!
NOTE: isNaN(x) returns true if x is NaN, i.e. "Not a Number".
(!isNaN(request))
if (request > balance)
{
alert("Insufficient funds!");
}
else
{
balance = balance - request;
withdrawn = request;
}
}
// Print receipt
alert("Receipt: $" + withdrawn + " withdrawn, balance is $" + balance);
Page 2 of 2
Download