Lab 4: loops

Programming the Web: An Introduction (Spring 2021)


Goals


Instructions


Examples to review

Open the folder lab4 in VS code. Then open loop-examples.js and lab4.js. Open loop-examples.html in your browser and open a console in your browser while viewing loop-examples.html and try evaluating the functions defined in loop-examples.js in that console.


JavaScript console exercises

Open the JavaScript console in your Web browser (Command-Option-I). Using the browser’s JavaScript console, try these examples, in order, one at a time:

> String(947);

> let c = 0;
> c = c + 1;
> c

> c += 2;
> c

> c = c - 3;
> c

> c -= 2;
> c

> 'A' < 'B'

> 'A' === 'a'

> 'A' < 'a'

> 'a very lengthy string' > 'short'

> 'Kiwi' > 'apple'

> 'ZZZZ' < 'a'

> let i = 1;
> while (i <= 5 ) {
      console.log(i);
      i += 1;
  }

> phrase = 'Abc';

> phrase.charCodeAt(0)

> phrase.charCodeAt(1)

> phrase.charCodeAt(2)

> phrase.charCodeAt(3)


> String.fromCharCode(33)

> String.fromCharCode(50)

> String.fromCharCode(65)

> String.fromCharCode(97)

> let j = 48;
> while (j < 58) {
      console.log(j + ' : ' + String.fromCharCode(j));
      j += 1;
  }

> j = 97;
> while (j < 110) {
      console.log(j + ' : ' + String.fromCharCode(j));
      j += 1;
  }

Programming problems

Write your code in lab4.js.

  1. Complete factorial(n) so that it returns the product of the integers from 1 through n (inclusive). Examples:

    > factorial(0)   // factorial of 0 is defined to be 1
      1
    > factorial(3)
      6
    > factorial(6)
      720
  2. Complete sumSquares(n) so that it returns the sum of the squares of the integers from 1 through n (inclusive). (Use a while loop.) Example:

    > sumSquares(4)   // 1*1 + 2*2 + 3*3 + 4*4
      30
  3. Complete mask(s) so that it returns a string consisting of the '*' symbol concatenated for the entire length of input string s. Examples:

    > mask('')
      ""
    > mask('hello')
      "*****"
  4. Complete padLeft(s, n, filler) so that given string s, number n and string filler, it returns a new string that has copies of filler placed in front of s so that the result has length at least n. It should pad the string with no more copies of filler than necessary. Examples:

    > padLeft('Mike', 10, '*')
      "******Mike"
    > padLeft('413', 7, ' ')
      "    413"
    > padLeft('NOT', 6, 'ok')
      "okokNOT"
    > padLieft('nothing needed', 8, 'x')
      "nothing needed"
  5. Complete firstDigit(n) so that it returns the leading (most significant) digit of n. Assumes n is an integer that is positive or 0. Examples:

    > firstDigit(0)
      0
    > firstDigit(9)
      9
    > firstDigit(537)
      5
    > firstDigit(123456789)
      1

    Hint: repeatedly divide by 10 and use Math.floor.

  6. Complete snakeEyes() so that it repeatedly simulates rolling two six-sided dice until they “land as snake eyes” - meaning the sum of the dice is 2. Return the number of rolls required to arrive at snake eyes. In a comment, explain whether the loop you use is definite or indefinite. Your code should call the function randomRange which has been supplied for you.

  7. Complete slice(s, start, stop) so that it returns the slice (a.k.a. substring) of s that starts at position start and runs up to but does not include position stop. Assume s is a string and start and stop are nonnegative integers. For example:

    > slice('hello world!', 3, 7)
      "lo w"
    > slice('nothing to see', 5, 5)
      ""
    > slice('All!', 0, 4)
      "All!"
  8. Complete countOccurrences(s, c) so that it returns the number of times character c occurs in string s. Examples:

    > countOccurrences('To be or not to be.', 'o')
      4
    > countOccurrences('To be or not to be.', 'B')   // case sensitive!
      0
  9. Complete indexOfChar(s, searchChar) so that it returns the first position in s where searchChar occurs. If it does not occur, it should return -1. Examples:

    > indexOfChar('Not here!', 'x')
      -1
    > indexOfChar('Right here!', 'R')
      0
    > indexOfChar('Right here!', 'r')
      8

    Recall that m is a factor of n if m divides into n without any remainder. For example, the factors of 36 are: 1, 2, 3, 4, 6, 9, 12, 18 and 36 itself. A number is perfect if it is the sum of all its factors other than itself. (Perfect numbers are quite fascinating!) As we have seen, we can test if a number d is a factor of a number q by using the remainder operator and comparing the result to zero:

      > 36 % 12 === 0   // 12 is a factor of 36
      true
      > 36 % 11 === 0   // 11 is not a factor of 36
      false
  10. Complete isPerfect(n) so that it returns true if n is a perfect number and returns false otherwise. Examples:

    > isPerfect(6)   // 6 = 1 + 2 + 3
    true
    > isPerfect(15)  // 15's factors are 1, 3, and 5
    false

  11. Complete allSame(s) so that it returns true if all the characters in string s are the same. Examples:

    > allSame('')
      true
    > allSame('x')
      true
    > allSame('NOOOOOO')
      false
    > allSame('******')
      true
  12. Complete table(n) so that it returns a string representation of an n-by-n multiplication table. Use nested while loops and call padLeft to help format the table into columns. Examples:

    > console.log(table(3))
        1    2    3
        2    4    6
        3    6    9
    
    > console.log(table(12))
        1    2    3    4    5    6    7    8    9   10   11   12
        2    4    6    8   10   12   14   16   18   20   22   24
        3    6    9   12   15   18   21   24   27   30   33   36
        4    8   12   16   20   24   28   32   36   40   44   48
        5   10   15   20   25   30   35   40   45   50   55   60
        6   12   18   24   30   36   42   48   54   60   66   72
        7   14   21   28   35   42   49   56   63   70   77   84
        8   16   24   32   40   48   56   64   72   80   88   96
        9   18   27   36   45   54   63   72   81   90   99  108
       10   20   30   40   50   60   70   80   90  100  110  120
       11   22   33   44   55   66   77   88   99  110  121  132
       12   24   36   48   60   72   84   96  108  120  132  144

As usual, to experiment directly with my solution, you can open up a console while viewing this page and try examples such as:

> padLeft('Mike', 10, '*')
  "******Mike"

Challenge problems