Assignments JS.Loops

Handing In Assignments

You must hand in by using git

Hand In by git

  • Create an empty repo on bitbucket.org, gitlab.com, or github.com,
  • git push your local repo to the above remote repo.
  • For node assignments please put the line(s)

    node_modules/

    into your .gitignore file.

Send a mail to with:

  • The word 'handin <subjectname>' in the subject line of your mail
  • The url of your repo(s).

Assignment JS.Loops.2.2 Functions

Definition

A natural number p is called prime if p ≠ 1 and the only numbers that divide p are 1 and p itself.

Fundamental Theorem of Arithmetic

Every natural number greater than 1 is either prime or can be written uniquely as a product of prime numbers.

There is a nice proof of that theorem but since this is no math class, we shall skip that, and just trust that it is so, and use it.

Now you must solve at least number 1 of the following. If you have the energy and skill you may want to have a go at the next ones too, but please solve the rest of this chapter's assignments before spending time on that.

  1. Write a function isPrime(p) that returns true if p is prime, and false otherwise.
  2. Write a function that creates a collection of the primes dividing p if isPrime(p) === false.
  3. Write a procedure that lists the primes dividing p if isPrime(p) === false.
  4. If you haven't already done so in number 2 above, please augment that function so that it accepts and reflects that a prime may divide p more than once.

Write an HTML5 page that allows for testing your function(s).

Hint for beginners: You may prefer to work with embedded JavaScript while you test your code. Once it works as desired, separate the JavaScript into an individual .js file, test again, then hand in your solution.

Assignment JS.Loops.71

Example 6.8. jsloops71.js

[Dow16] Exercise 7.1

'use strict';
/* Downey: thinkjava, ex 71 */
const loop = function(n) {
    let i = n;
    while (i > 1) {
        console.log(i);
        if (i % 2 == 0) {
            i = i / 2;
        } else {
            i = i + 1;
        }
    }
}

loop(10);

  1. Draw a table that shows the value of the variables i and n during the execution of loop. The table should contain one column for each variable and one line for each iteration. You might trick the program into doing it for you.
  2. What is the output of this program?
  3. Can you prove/rationalize that this loop terminates for any positive value of n?

Assignment JS.Loops.72.1

[Hav19] Exercise FizzBuzz, chapter 2.

Write a program that uses console.log to print all the numbers from 1 to 100, with two exceptions. For numbers divisible by 3, print "Fizz" instead of the number, and for numbers divisible by 5 (and not 3), print "Buzz" instead. Save as fizzbuzz.js.

When you have that working, modify your program to print "FizzBuzz" for numbers that are divisible by both 3 and 5 (and still print "Fizz" or "Buzz" for numbers divisible by only one of those). Save as fizzbuzzBetter.js. (This is actually a job interview question that has been claimed to weed out a significant percentage of programmer candidates. So if you solved it, your labor market value just went up.)

Assignment JS.Loops.73

Write an iterative function powi(r, e) that calculates the e-th power of r, re. Document it with a couple of testcases. Save the function in myFuncLib.js.

Assignment JS.Loops.74

Write an iterative function facti(n) that calculates n!. Document it with a couple of testcases. Save the function in myFuncLib.js.

Assignment JS.Loops.75

We have flipped coins and dies 10 times. I want you to change the code so that the user may enter the number of times she wants to play dice, say 25, 1000, or even 100000000. Write a jsPlay75.html that does that, and of course uses the function we created earlier.

Change the program so that user can choose between flipping coins, rolling dice, playing roulette, etc.

Save them in a repo games.