Introduction to (Web) Programming

Lab 5: bits of binary

shortcut to main problems

Instructions

• Work on this lab with your partner (or on your own if you have not been assigned one).
• Download the starter version of lab5.js and the basic lab5.html for running and testing it in your browser.
• Write all your code in the file lab5.js.
• Unless otherwise indicated, use only the JavaScript operations and commands we have described thus far in class.
• Show your work for a given problem to the instructor or one of the lab assistants before moving on to the next problem, but do not get too bogged down on any one problem.

JavaScript console exercises

Using the browser's JavaScript console, try these examples, in order, one at a time:

> let n = 7;
> n

> n = 5;
> n

> n += 3;
> n

> const DAYS_PER_WEEK = 7;
> DAYS_PER_WEEK

> DAYS_PER_WEEK = 6;
> DAYS_PER_WEEK

> n

> n.toString()    // show n as a string version of the number (implicitly base 10)

> n.toString(10)  // again, but explicitly indicating base 10

> n.toString(2)   // show n's value as binary string

> parseInt('11')      // implicitly tells JavaScript we are referring to base 10

> parseInt('11', 10)  // explicitly indicates base 10

> parseInt('11', 2)   // here we mean base 2

> let i = 0;
> while (i < 20) {
let base10 = i.toString();
let base2 = i.toString(2);
console.log(base10 + ' : ' + base2);
i++;
}

> let s = 'AaBbCcDd';
> i = 0;
> while (i < s.length) {
let c = s.charAt(i);
let code = s.charCodeAt(i);
let base10 = code.toString();
let base2 = code.toString(2);
console.log(c + ' : ' + base10 + ' : ' + base2);
i++;
}

Main exercises

Solve these problems by working in lab5.js and reloading lab5.html in your browser as you make changes.

Begin with three debugging exercises:

1. Fix reverse(s) so that it correctly returns the a string that is the reverse of string s. For example:

> reverse('')
""
> reverse('R2D2')
"2D2R"
> reverse('Computer')
"retupmoC"
2. Fix isBits(s) so that it correctly returns true if s is a bitstring (all its characters are either zeroes or ones) and false otherwise. Note: it is acceptable for the string to be empty. Examples:

> isBits('')
true
> isBits('00100111')
true
> isBits('01210')
false
> isBits('whatever')
false
3. Fix isPow2(n) so that it correctly returns true if n is a power of 2 and false otherwise. Examples:

> isPow2(0)
false
> isPow2(1)
true
> isPow2(2)
true
> isPow2(4)
true
> isPow2(8)
true
> isPow2(15)
false
> isPow2(32768)
true
> isPow2(96)
false

4. Complete the function maxPow2(n) so that, it returns the largest power of 2 that is less than or equal to n. You can assume n is a number greater than or equal to 1. Examples:

> maxPow2(1)
1
> maxPow2(2)
2
> maxPow2(3)
2
> maxPow2(4)
4
> maxPow2(27)
16
> maxPow2(40000)
32768
5. Complete the function flipBits(s) so that it returns a new string the same length as s, but where all '0's are replaced with '1's, all '1's are replaced with '0's and all other characters remain as is. Examples:

> flipBits('')
""
> flipBits('1100001')
"0011110"
> flipBits('0123210')
"1023201"
> flipBits('1 or 0')
"0 or 1"
6. Complete removeLeading0s(s) so that it returns a new string that is like s except it has all leading '0' characters removed. By "leading", we mean all that occur consecutively beginning at position 0. If there are no leading zeroes, the string returned should be identical to s. Examples:

"1"
"1"
"whatever000"
"123456789"
> removeLeading0s('x01')     // does not lead with 0
"x01"
> removeLeading0s('   01')   // neither does this!
"   01"

If you can, make your code preserve a final '0' (if the string only consists of '0's) so that:

"0"
"0"
7. Complete isValidBinary(s) so that it returns true is s is a string representing a valid binary number meaning it is not empty, consists only of the characters '1' and '0' and has no superfluous leading zeroes. Examples:

> isValidBinary('')
false
> isValidBinary('0')           // this 0 is not superfluous
true
> isValidBinary('1')
true
> isValidBinary('10100111')
true
> isValidBinary('010100111')   // superfluous leading 0
false
> isValidBinary('101001211')
false

To experiment directly with my solutions, you can open up a console while viewing this page and try examples such as:

> reverse('R2D2')
"2D2R"
> isValidBinary('100111')
true