Programming Homework
Deadline: May 1st
For the exercises, only do the first 19 questions
and then take a screenshot. Apparently, some exercises jump back to the
questions list as soon as you correctly solve all 20 problems and you
don't get a chance to take a screenshot.
RUN ALL YOUR CODE ON W3SCHOOLS/THIMBLE BEFORE SUBMITTING. ONLY
SUBMIT CODE THAT RUNS. IF YOUR CODE DOESNT RUN FIGURE OUT WHY. THE ONLY
WAY TO LEARN PROGRAMMING IS BY DOING IT RIGHT. READ THE TUTORIALS ON
W3SCHOOLS AS WELL AS THE LINKED BOOK. UNDERSTAND THAT THIS IS A
PRACTICE RUN FOR YOUR TERM PROJECT. THE GOAL IS TO WRITE COMPLETE
WORKING PROGRAMS, NOT TO CUT CORNERS.
For this assignment, you'll be teaching yourself programming using
javascript. The reference materials you'll be using are as follows:
IMPORTANT: For each programming question, you must include a short
English paragraph that explains how you intend on solving the problem. And
your code should reflect what your paragraph says.
How to do the assignment
- For the exercises on the smarterwaytolearn website, do them, take
the final screenshots showing all green numbers, and attach them to your
email. Make sure to give them proper names. So for eg, for exercise 1, the
screenshot will be called 1, etc. On a Mac, you take a screenshot by
pressing command-shift-3. On a PC, you hit the print screen key, and
on linux, you compile your graphics driver first :D
- For programming questions, use thimble, or the w3schools scratch pad
and once your code runs, copy and paste it into the body of the
email.
Visit http://www.asmarterwaytolearn.com/js/index-of-exercises.html
and do exercises 2, 3, 4, 5, 7, 8, 9, and 10. Make sure all of the numbers
on top are green for each exercise.
NOTE: In the class, we discussed six comparison operators: <, <=, >,
>=, ==, and !=. This website uses slightly more sophisticated versions of
equal to and not equal to. It uses === and !==. You can read
all about them here: Javascript
Comparisons
Create a variable called my_var and set it equal to 40.
Create a second variable and get its value from the user. Create
a third variable that is equal to the sum of the first and second
variable. Create a fourth variable that is half of thr sum of thr
first and the second variable. Create a fifth variable that is
equal to the first variable minus the second variable. Finally,
print out all the variables with appropriate messages.
What's wrong with the following statement? What would be the
correct way of saying it?
3 = x
Write a program that asks the user for a number and then prints the
cube of that number.
Ask the user for two numbers. The first one goes in num1, the second
one goes in num2. Then swap the variables (as in the value of num1 goes in
num2 and vice versa.
Write a program that asks the user's age and then prints how long
they've lived in seconds.
Write a program that asks the user if they're a good person or a bad
person. If the user enters "good" tell them to stop lying. If they enter
"bad" tell them that at least they're honest.
Did you know that you can ask Javascript to get the current time for
you? You need to use the following two lines:
var d = new Date();
var n = d.toTimeString();
And then, you can print n.
Write a program that asks the user if they want to know the time. If the
user says yes, display the time. If the user says no, don't do
anything.
Did you know that you can use if statements inside other if
statements? For eg, you can have something that looks like this:
if(...){
if(...){ //first nested if statement.
...
...
}
if(...){ //nested if-else inside an if-else.
...
}
else{
...
}
}
else{
if(...){ //another nested if!
...
}
}
Write a program that works as a bouncer at a club. You ask the user their
age. If the user is 18 or older, print a message that says "you may
enter". If the user is less than 18, ask them for a bribe. If the bribe is
20 or more, print the message "you may enter". Otherwise, print the
message "You're too young."