CS312 - Assignment one

Due: 2017-02-23 11:00a

Topics

Problems

  1. [5 points]One of the places where people get tripped up in JavaScript is with "falsy" and "truthy" values. Do some searches and read up on them. Write a function howFalse(a), which takes an array as an argument. It should then return an object containing a count of each kind of falsy value found in the array. The object should only have properties for falsy values that were actually present. For example, howFalse([5, 0, 42, 'dalek', 3-3, 7, Math.sqrt(-1)]) should return {'0': 2, NaN: 1}.

  2. [5 points] In JavaScript, functions can take an optional number of arguments. For example, if you look at the description of Math.min, you will see that it can take an arbitrary number of values for comparison. Write a function called myMin() that duplicates this functionality (without just calling Math.min).

  3. [5 points] Find the largest value in a list using reduce

    Write a function called reduceMax. When your function is passed as an argument to an array's reduce method, it should return the largest element in the array (you can assume that the array has numeric data).

  4. [12 points] Find the largest common substring between two strings

    I would like you to write a function called largestCommonSubstring(str1, str2). It should (obviously) return the largest common substring. The substring does not need to be continuous, just in the same order. For example, largestCommonSubstring('afbwc', 'yabzcx') => 'abc'.

    There are multiple ways to tackle this problem, but I would like you to work the problem from the top down using recursion. Consider a function f(i,j) that takes as arguments the indices of the last character in each string. As a base case, if either one of those indices is less than zero, then we know there is no common substring. We then have two recursive cases. If the characters are the same, we know that the character appears at the end of the common substring, so we can tack it on the end of the longest substring of the rest of the two strings (which we find recursively with f(i-1, j-1)). If the last character doesn't agree, then the longest substring is the longer of f(i, j-1) or f(i-1, j) (i.e., we can throw away the last character from one of the two lists, but we don't know which). [Note: while we could have passed the actual strings around and chopped them up as we went with the built-in substring method, working with indices will be easier and more efficient].

    You could write this recursive function fairly easily, but it will have horrible performance (exponential, like fibonacci). So, I would like you to use a closure and memoization (not a typo), to store values you have seen before to short-circuit long recursive chains. Create a 2D array that allows you to look up the longest substring given an i,j pair. Note that the longest common substring is not unique, but if you use this technique, you should all return the same substrings.

  5. [8 points] In class, we briefly used a standard for loop, which looked just like a Java/C style for loop. JavaScript has two other types of for loops which at least on the surface more closely resemble Python for loops. What are they? How are they different from a standard for loop or a Python for loop? Write a function called forDemo() which illustrates what these two types of for loops do. Use console.log() to print out explanatory text (i.e., it should be obvious from reading the output which loop is producing which output and what the source data looks like).


Put all of these functions in a file called username_hw1.js, where username is your Middlebury username. I expect the file to be well commented, including your name, date, and assignment number in a comment at the top of the file. Your file should be submitted on Canvas.

As hopefully you noticed in class, you can run your code interactively in Node.js by calling .load *filename* once you are at the command line interface. Do this to load your functions for testing. You can also just put some testing code on the bottom of the file and run your file through Node non-interactively: node *filename*. However, if you do this, please comment out this test code before handing it in.