Introduction

In lecture on Wednesday, Wade introduced the push method of an Array, allowing us to add data onto an existing array. In lab, you will go through a problem very similar to what you saw in lecture.

Download the Lab

To get started, first download the ZIP file for Lab 5 here.

Motivation

Last year the following comic from xkcd.com went viral when it was brutally cold for several days in a row in the midwest:

xkcd.com
Source: http://www.xkcd.com/1321/

Ignoring any larger implications made by the comic, lets look at the data by doing the data analysis ourselves!

Completing the Lab

Part 1: Understanding the Input Data

We have already provided for you input data for this lab, which will come to you in the following format:

[
  { year: 2015, lows: [ 32, 19, 30, 23, ... ] },
  { year: 2014, lows: [  8, 21, 18, 24, ... ] ),
  ...
]

That is, the input is an Array where each element is an Object that has two variables:

Part 2: Understanding the Output Format

To complete this lab, your function must return an Array of Objects:

[
  { year: 2015, zeroDegreeDays: 3 },
  { year: 2014, zeroDegreeDays: 14 ),
  { year: 2014, zeroDegreeDays: 0 ),
  ...
]

That is, the return value is an Array where each element is an Object that has two variables:

Part 3: Completing the Zero Degree Days Function

As part of completing this lab, you will need to process the lows that come as part of the input data. As a reminder, this array will be an array of low temperatures and have the following format:

[ 32, 19, 30, 23, ... ]

Complete the findZeroDegreeDays function in lab5.js so that the function returns the number of days where the low temperature was below zero (0) degrees. This is very similar to the function you wrote in last week and requires one for-loop and one if-statement.

Test your code now!

We have included test cases that test only Part 3. Make sure that the Lab webpage indicates to you that Part 3 works before going forward! You will be using the function you created in Part 3 to finish Part 4.

Part 4: Finishing the Lab

Inside of lab5.js you will find a second function: analyzeWeather. To successfully complete this function, you will need to:

  1. Create an empty array, which you will use to construct the return value. Re-read Part 2 of this lab to remind yourself of the format that the return value must be in.
  2. Creating a for-loop to loop through the input
  3. Create a new variable that stores a single object from the array based on the current index in the for-loop
    • This variable will store different data each time the loop runs
    • We will use this new variable inside of this for loop
  4. Call the findZeroDegreeDays function with the array of low temperatures. This function will return the zero degree days (a Number) that you will need to use later.
  5. Create an object inside of the for-loop that contains name-value pairs for both year and zeroDegreeDays. This is exactly like what we did in lecture on Wednesday!
  6. Use the .push function you saw in lecture on Wednesday to push the newly created object onto an array to be returned after you have finished processing each piece of data.

Submission

To submit your lab, you must submit your lab5.js to the CS 105 website. You should submit ONLY .js file. You do not need to submit the other files in the folder. Click here to submit the lab!