30.4. Assignments DE.3

Submission
You must submit assignments using git
Create Repo
  • 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 the line node_modules/must be in your .gitignore file.
Submit
By email to with:
  • The word 'submission <subjectname>' in the subject line
  • The url(s) of your repo(s) in the body

30.4.1. Assignment DE.3.0

I will give you a naive example of a function that returns true if a given number is prime, and false if it is not.

Example 30.1. Naive isPrime
const isPrime = function (arg) {
    for (var i = 2; i < arg; i += 1) {
        if (arg % i === 0) {
            return false;
        }
    }
    return true;
}

Your assignment is as follows:

  1. Place the given function in a JavaScript library file.
  2. Write a simple HTML5 document and include the library in the head.
  3. Write another js-file that requires the user to enter a positive integer with max-size of 253-1 = 9007199254740991, eg 9007195909437503 :)

    Then it must call isPrime with the entered number as input. The result must be displayed on the page. The code must time the function as follows:

    let start = new Date();
    let b = isPrime(arg);
    let stop = new Date();
    let elapsed = stop - start;
  4. Put the three files into their own repo. Test, commit, and correct until it works.
  5. Create a git branch and change isPrime in the branch to a less naive, and faster version. Test, commit, and correct until it works.
  6. Create a second git branch from the previous branch, and change isPrime in the branch to an even less naive, and faster version. Test, commit, and correct until it works.

Hand in the repo with all three branches. This means that you have three working versions of isPrime.