Due Date: July 5 before midnight;
Max Points: 100
Important Reminder: As per the course Academic Honesty Statement, cheating of any kind will minimally result in your letter grade for the entire course being reduced by one level.
To be turned in via the submission link on brightspace.
Please remember to justify all answers.
You are encouraged to use the web or the library but are required to cite any external sources used in your answers.
It may be the case that some questions cannot be answered as requested.
The solution to Project 1 implements error recovery using an internal undo facility. Discuss how you could extend this internal facility to an externally visible undo-redo facility. 10-points
Many of the functions passed as parameters to functions in the
JavaScript standard library provide the invoking object as a
parameter. For example, when
Array:map() is used as in array.map(f)
, the third
parameter passed to f()
will be array
. Give an example of a
situation where this behavior is useful. 5-points
Assume that you are a evil spammer. Discuss how would you use
JavaScript regular expressions within a program to harvest email
addresses from a document. Your program should cope with anti-spam
workarounds deployed by users like spelling out email addresses
using sequences like user at example dot com
. Your answer
should deal only with the use of regex's to extract the email
addresses; you may assume that the contents of the document are
available as a string. 10-points
Discuss how you would implement a random()
function in
JavaScript (without using any library random functions). The
details of the random number generation algorithm are unimportant;
what is required is the setting up of the function so that
subsequent calls usually return different results. 5-points
Assuming no earlier variable declarations, the following
JavaScript code will output 4
when run in non-strict mode.
x = 1; obj1 = { x: 2, f: function() { return this.x; } } obj2 = { x: 3, f: function() { return this.x; } } f = obj1.f.bind(obj2); console.log(obj1.f() - obj2.f() + obj2.f.call(obj1) + f());
Explain why this is so. 10-points
Novice programmer Ben Novitia asks you for help in identifying the problem in the following nodejs code:
... const DIR = `${process.env.HOME}/my-stuff`; function readFile(filename) { fs.readFile(`${DIR}/${filename}`, function(err, result) { return result; }); } const contents = readFile('test.data'); console.log(contents);
What do you tell Ben? 5-points
Multiple objects obj1
and obj2
have a common interface \(I\)
(i.e. the same set of methods) but all the methods in obj1
are async
whereas the methods in obj2
are synchronous.
There is a function f(obj1)
which is set up to expect a
read-only obj1
argument; so the code for f()
assumes that
obj1
methods always return promises. Hence calling f(obj2)
would not work because even though obj2
has the same methods
as obj1
, those obj2
methods do not return promises.
How would you set things up so that f(obj2')
can be successfully
called, where obj2'
is derived from obj2
.
You answer should not assume anything about \(I\), obj1
, obj2
or f()
; in particular, you should not assume that you have
access to the code for obj1
, obj2
or f()
. 15-points
The following is an attempt to log the connection information for a mongo database:
const MONGO_URL = 'mongodb://localhost:27017/data'; ... function getConnection(url=MONGO_URL) { const result = mongo.connect(url); return result; } const conn = getConnection(); console.log(conn);
Assuming that mongo
contains a MongoDB client object, what
is wrong with the above code? 5-points
A web application needs to make asynchronous calls to independent web services \(s_1\) and \(s_2\), followed by an asynchronous call to a web service \(s_3\) which combines the results from \(s_1\) and \(s_2\) into a single result.
How would you set this up so as to maximize performance.
Assume that web service \(s_1\) is not particularly reliable and that an alternate web service \(s_1'\) is available which is functionally equivalent to \(s_1\). How would you modify your answer to the previous question to make the application more robust by using the results from either \(s_1\) or \(s_1'\) depending on which of \(s_1\) or \(s_1'\) responds? 15-points
Write a function fib(n)
which can be used as in
for (f of fib(6)) console.log(f);
to print out successive Fibonacci numbers 1, 1, 2, 3, 5, 8. The
return value of fib()
is not allowed to be any kind of
collection. 5-points
Discuss the validity of the following statements. What is more important than whether you ultimately classify the statement as true or false is your justification for arriving at your conclusion. 15-points
this
for a fat-arrow function can be changed using call()
.
The await
keyword can only be used within a function
declared async
.
A JavaScript event handler will run soon after the event is received.
A function declared async
can be called without using an
await
.
A function called using await
must have been declared async
.