Skip to content

portlandcodeschool/jse-fall15-1

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

8 Commits
 
 
 
 
 
 
 
 

Repository files navigation

Homework #1

(Due Monday 7/20)

You need to turn in this homework by cloning this repo, making a new branch with your solutions, pushing to github, and issuing a pull request. For details, follow the instructions here.

Please also read the learning objectives for this week.


1) (30% of total time) Predict the output of each of the following expressions, and explain the rules which determine that answer. (Hint: The rules for operator == are complicated, but do your best!)

If the output depends on the value or type of variable x, identify the conditions (what x is) when the expression will output true (or false, if that's simpler). Assume the cases are independent, and x is reset to an unknown value before each.

Some of these are tricky! Don't trust your first instinct.

a) "1" == 1

b) "1" === 1

c) x == 'x'

d) x == (x+'')

e) '' == ' '

f) x = true

g) var x; x == 'undefined'

h) '9'<'10'

i) typeof x + 1 === "number"

j) typeof x % 2 === "number"

k) typeof (x % 2) === "number"

l) x++ == ++x

m) ++x == x++

n) "1"+x == 1+x

o) "0"+1 == 1

p) (typeof (x+1))===(typeof x)

q) (x*1 == x) || ((typeof x) != "number")

r) (x=(typeof (x+(typeof x))))==x


All of the following can be solved with ordinary expressions and global variables with primitive values. You don't need functions, loops, or other topics beyond our first two classes.

2) (10%)

Assume variables x, y, and z are numbers.

a) Write an expression for the mean (i.e. average) of x, y, and z.

b) Write a series of expressions to adjust each of x, y, and z halfway toward the mean of the three. That is, reset the value of each variable to something new based on its previous value.


3) (20%)

Suppose you're encoding geometric shapes in a Cartesian (2D) coordinate system, and you represent a rectangle with four numeric variables:

  • l : horizontal position of left edge (relative to some origin);
  • r : horizontal position of right edge;
  • t : vertical position of top edge;
  • b : vertical position of bottom edge.

a) Write an expression for the rectangle's area.

b) Write an expression which is true if the rectangle is taller than it is wide, and false otherwise.

c) Write an expression for the circumference of the biggest circle which can fit inside the rectangle. (Hint: you'll need logic similar to that in b.)

d) Write an expression for the area of the smallest circle which completely encloses (i.e. circumscribes) the rectangle.

e) Imagine subdividing your rectangle into 3 equal rows and 3 equal columns, which would create 9 smaller rectangles, identical in shape but varying by position. Define four new variables describing the centermost small rectangle. (Hint: one of the many solutions is very similar to the solution of 2b above.)


4) (25%)

Imagine that the squares of an ordinary checkerboard are numbered in two different ways:

  • Each square has integer coordinates (R,C) describing its row and column. Both values should be in the range 0..7, so that the upper-left square is at (0,0) and the bottom-right is at (7,7).

  • Each square has a unique integer number N from 0 to 63. These numbers run sequentially left-to-right one row at a time, top to bottom. Therefore the upper-left square has N===0 and the bottom-right has N===63.

a) Given a particular R and C, find the corresponding N. That is, write an expression for variable N containing variables R and C.

b) Given N, find R. Write an expression for R which contains N.

c) Given N, find C. Write an expression for C which contains N.

d) Assume the squares are colored black and white, with the upper-left square black. Write an expression (or pair of conditional statements) to set a variable color to either 'black' or 'white', describing the square identified by variables R,C, and N (or a subset of them, if you don't need all three).


5) (15%)

Suppose you represent a fraction (n/d) with 2 integer variables: n for the numerator and d for the denominator. If n is greater than d, the fraction is "improper", but it can be rewritten as a proper fraction. For example, "7/4" is improper, but it can be rewritten as "1 3/4", which is proper.

Assuming variables n and d are defined in advance (but you don't know their values), write a series of expressions to generate a string expressing the proper form of the fraction n/d. For example, when n===7 and d===4, your resulting string should be "1 3/4". You may assume both n and d are positive integers and n > d, but otherwise you should be able to handle any values of n and d.

a) Solve it first by making use of a function called Math.floor.

b) Now solve it without calling any functions, using merely operators. (Hint: you'll need at least the modulo operator %.)

About

Javascript evening course (fall 2015) homework, week 1

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published