Boolean Operators Part 3: Short-circuting

Consider the following fragments of code and try to follow the logic of their execution and their outcome:

true || arglebarg

if (doJShomework() || runMarathon()) deserveCookie();

if (doJShomework() && runMarathon()) deserveCookie();

(backup = '') && deleteAllFiles()

treat() || trick()

practice() && win() && celebrate()

While loops

Consider the following fragments of code: execute them by hand, as shown in class, and then compare your answers to what you get from running the examples in scratchpad. The second is almost the same as the first. Do you understand why they behave differently?

Afternoon: Mixed Practice

Nested Loops: Number Square, Number Pyramid

Write a set of nested loops that will console.log a square of numbers. You should be able to change a single loop's initial value to change the size of the square. Sample output:

012345
012345
012345
012345
012345
012345

Now do something similar, but with a triangle of numbers:

0
10
210
3210
43210
543210

Simplifying code

Rewrite each block below as the simplest equivalent you can discover. Your equivalent should produce the same final conditions as the original code given the same initial conditions.
In each case, if there is an undeclared variable, assume it has been declared earlier and set to an unknown value.

Simulating &&

Suppose the '&' key on your keyboard is missing, and you want to be able to continue programming without it. You decide to simulate the && operator with an expression!

a)
Write an expression that tries to simulate the && operator: it should always return the same result as (a && b) for any values of a and b. (For example, given initial conditions var a = (0>1); and var b = true;, your expression using a and b should return false.) But you can't use && itself within your expression!

b)
Write another expression that tries to simulate a double-&& operator: it should always return the same result as (a && b && c) for any values of a,b,c. (For example, given initial conditions var a = 1 > 0; var b = 0 > 1; var c = true; your expression using a,b, and c should return false.) As before, you're not allowed to use && itself.

Checkerboard

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).

e) Print the board to a string, using "#" for black squares and " " for white squares.

Prime numbers

Prime numbers are central to cryptography, and frequently used in other fields of science. Write an expression that will make use of a variable num and determine if it is a prime number or not. As a reminder, a prime number is any number that is evenly divisible only by itself and 1. The first ten primes are 2, 3, 5, 7, 11, 13, 17, 19, 23, 29.

Roman Nvmerals

Write an expression that will convert a variable num to Roman numerals. Your expression should console.log a string that is the number represented as Roman numerals. For reference:

You may assume that the num variable is a positive integer under 4000.

nth Fibonacci number

The Fibonacci sequence is a sequence of numbers in which each number is the sum of the two previous numbers in the sequence, for example: 0, 1, 1, 2, 3, 5, 8, 13 .... Write an expression that assumes a positive integer variable num and evaluates to the corresponding Fibonacci number--if num = 7, the expression should console.log 8.

Calendar

Given a starting day of the month (numbered 0-6, so that if the first of a month falls on a Tuesday you will get a 2) and the number of days in that month, write an expression that will create a string that represents that calendar month. For example, given var numDays = 31; var startsOn = 3, your expression should generate something like: