Assignment: Ticker

In this assignment, you will write some Javascript code for collecting stock data, computing some basic statistics from this data, and plotting this data along with the statistics.

Before you get started, you’ll need to disable a security feature on Firefox that prevents Javascript from reading data from files on your computer. Open Firefox and type about:config into the URL bar. Agree to the scary dialog about voiding your warranty and do a search on the page for: security.fileuri.strict_origin_policy. Change the final value on the line you find to false.

Here is the provided code, which you should unzip to get started. To begin, we provide you with the shell of your main function:

// your helper functions go here

function ticker() {
    var ticker = document.getElementById('ticker').value;
    var price = getData( ticker );
    // your code goes here
}    

The function ticker will be run when the user clicks on the button “Graph It” on the web page ticker.html. To make sure this is working, you can do the following:

  1. Open the webpage ticker.html with your browser. Open up the Firefox console so that you can see the results of calls to console.log.

  2. Enter the symbol APPL into the textbox to the left of the “Graph It” button. Click ‘Graph It’. In the console, a list like prices: (251) [105.260002, 107.32, 108.739998, 106.82, 108.029999,... should appear. If so, everything is working correctly.

The first line of this code retrieves the ticker symbol from the text window in the browser. The second line of code retrieves the data from a file and returns to you an array price containing the stock data (closing stock price for each day of 2015). If we don’t have the data for the specified ticker symbol, then price will be an empty array [].

Once you have retrieved the stock data you should first check to make sure that it has some data. If the array is empty, then you should simply call our function plotTicker as follows:

plotTicker( "", [], 0, 0, 0 );

This will clear out the previous plot and return an error message.

If the array is not empty, then you should compute the minimum, maximum, and average value for the array. Write three separate functions to do this, and have each function return a single value; call these functions from within Once you have these three values, you should call them from within the function ticker to retrieve the three values. Then call our function plotTicker as follows:

plotTicker( ticker, price, min, max, avg );

where min, max, avg are the variables that you populate with the appropriate values.

You can test your code with one of the following ticker symbols: AMZN, APPL, FB, GOOG, IBM, MSFT, NFLX, TWTR, or YHOO

Bonus: Drone control

This section of the assignment is a bonus, worth up to 2 extra points, and is not required. It will give you a bit more practice with conditionals and loops. If you choose to accept the mission, you’ll program a drone to fly safely within a region, avoiding dangerous clouds that might cause the drone to get lost. For all parts of this assignment:

To get started, grab the provided code.

Part 1: wanderer

Roomba vacuum-cleaners and pool-cleaning robots use randomness to navigate an unknown environment.

Open wanderer.html with Firefox. You should see a drone and several randomly placed clouds.

In wanderer.js, write the function wanderer, that causes the drone to do one of three actions: with probability .6, the drone should go forwards, if it is not blocked; with probability .2, the drone should turn right, and with probability .2, the drone should turn left. One way to enforce the probabilities is to simulate rolling a die with the random number generator. If you roll a die with 10 sides, and do the forward action for six of the outcomes, then the drone will go forward with probability .6.

Write a loop that causes the robot to take perhaps 50 actions. (Be careful to avoid an infinite loop!)

You can refresh the wanderer.html page in your browser to see and debug the robot motion, which you will want to do every time you change wanderer.js.

You might get some warnings from cloud9 about certain variables, such as blocked, being undefined. You can safely ignore these warnings, but if you see other warnings about your code, try to figure out how to correct the issue.

Part 2: hallway

For this problem, the drone is in a hallway of clouds that it must navigate through. A hallway is defined to be a sequence of empty squares arranged consecutively. For each empty square, the robot enters from one of the four sides, and leaves from a different side.

For example, if a drone uses the forward command to enter a square from the west, since we are in a hallway, we know that exactly one of the south, east, or north exits from the square must be open (not blocked).

So, to navigate a hallway, repeat:

  1. Enter a new square (by using the forward command)
  2. Find the exit for that square by using turning commands and the blocked function to test for a safe direction to move in.

Write code that, in an infinite while loop, executes the described algorithm. Your code should be general-purpose: it should work for any hallway, not just the one you were given.

If at some point your robot finds that all three of the potential exits from a square are blocked, it has found a dead-end. In the example given, the dead-end is the goal, and you should use the command return; to stop execution of the hallway function. This call to return is what will save your robot for looping forever.

Part 3: patrol

It is said that Theseus navigated the lair of the minotaur by keeping his hand on the right wall at all times. Edit patrol.js and view patrol.html for this problem.

Write code in the patrol function so that the robot traverses the outside border of the maze, keeping a wall or the boundary of the region to its right. Notice that since the robot can only sense walls in front of it, the robot may have to turn and check if there is a wall beside it on the right before taking any other action.

Submitting your code

Zip together a folder that should include your edited files as well as several provided files, and upload to Canvas as your solution.