Homework 03: Programming

Name (Last, First): ________________________
StudentID: __________________
Section: ________


Preparation

Print out your page and write your answers to the following questions. Use a separate sheet of paper if you need more space. The last problem requires an additional sheet of paper. Be sure to put your name, student ID, and section at the top of this page, on any attached pages, and securely fasten all sheets together with a staple. You may lose points if you do not do so! Have fun. [30 points total points!]

Questions

Most of the questions ask for "brief" answers.  This means you don't need to write more than a few sentences -- not whole essays!

Concepts
1.

Algorithms [10 points total]

a. In your own words, clearly explain what an algorithm is. [3 points]


b. Why is it important to have well-specified algorithms? What are algorithms useful for?
[2 points]






c. I have ten people standing in line, and I want to order them in alphabetical order by first
name, from “A” at the beginning of the line to “Z” at the end. Give a well-specified
algorithm to do so. These people are not too bright, so they will only respond to direct
orders/questions given to each of them individually (i.e. you can’t just yell at them
“Everybody get in line by alphabetical order!”), but you can use things like “for” loops.
[5 points]

 

 

 

 



2.

What is/are the benefit(s) of having functions? Why not just write all the code out in the place
where it is needed, instead of calling a function somewhere else to do the job? [2 points]

 

 



Javascript
For some of the following problems, it may behoove you to run it by creating an HTML page and placing the code in that page and loading it in Mozilla (and possibly using the Javascript Console), or by using W3School’s TryIt Editor (http://www.w3schools.com/js/tryit.asp?filename=tryjs_text) to test what the code does.
3.

Why will the Javascript code below not work? How should it be fixed? [3 points]

<script type="text/javascript">
document.write(greeting);
var greeting = "Ni hao!";
</script>

Note: 1 point for the correct script, 2 points for the explaination!

 

 

 

 

 



4.

I have a variable called myGrade that stores a number. Write an if-else statement that will give
an alert box that says “Woohoo!” if myGrade is greater than 3.4 and less than or equal to 4.0.
(Hint: you will need to review Boolean and relational operators.) Otherwise, it should give an alert
box that says “Boooo!” Please be careful with your capitalization, spelling, punctuation, etc. If
your code doesn’t run, you lose points. [4 points]

Note: 3 points for perfect! 1 point for almost correct if-else block. 1 point for the correct boolean
evaluation. 1 point for the alerts.

 

 

 

 

 



5.

Pretend you are writing the following code for an assignment. Copy and paste it somewhere, and
run it, as directed. It is supposed to count the number of both upper and lowercase s’s, and
then print out the total number, but as you will see when it is ran as is, it has many errors. Write down EACH
STEP of your debugging process. What did you first do to figure out where the errors are? What
are the errors? What steps did you do to figure out the error? Also, print out a copy of your
corrected code. [8 points]

<script type="text/javascript">

var myWord = 'supercalifragilisticexpialidocious';
var count = 0;

for(var i = 1; i > myWord.length; i++) {
     if(myWord.charAt(i) == 's') {
          count++;
     }
}
document.write('You have ' count + " s's.");

</script>

 

 

 

 

 



 

 

 

 

 

 

 

 

 

 

Turnin Notes

Collect all of the following pages and staple them together.  You can't be sure there will be a stapler in the lecture hall, so staple before you come to class!  Bring the package to class on Monday, October 30th , and turn it in at the beginning of class.  Keep in mind what the syllabus says about missed homework.   (Be sure your name and section are on anything you turn in, whether or not that is mentioned specifically.)