Unit 5 Lesson 12 Random Forecaster - Help

I have two comments/questions about the code for this project. I would appreciate some feedback

  1. Based on the instructions, the app should tell the weather tomorrow, but the Example solution uses forecastNumberList[i] == 1. This will actually pick up the weather for today.

  2. I do not understand why the function in the Example solution is traversing the cityList and using a conditional for the forecastNumberList. I feel the conditional should be based on the same list.

In my own version I used a different approach. I do understand that there are several ways to accomplish the same, I just do not understand what the code in the example does.

I traversed the forecastNumberList. When doing so, if i == 2, then I append the items at the same index for each of the other lists. I traverse the forecastNumberList and my conditional refers to the forecastNumberList. Please see the example below.

This is the link to my app.

Example
function filter(){
for(var i=0; i<cityList.length; i++){
if(forecastNumberList[i] == 1){
appendItem(filteredCityList, cityList[i]);
appendItem(filteredIconList, iconList[i]);
appendItem(filteredHighTempList, highTempList[i]);
appendItem(filteredLowTempList, lowTempList[i]);
appendItem(filteredConditionDescriptionList, conditionDescriptionList[i]);
}
}
}

My own
function filter() {
for (var i = 0; i < forecastsNumbers.length; i++) {
if (forecastsNumbers[i]==2) {
appendItem(filteredCitiesNames, citiesNames[i]);
appendItem(filteredConditions, conditions[i]);
appendItem(filteredIcons, icons[i]);
appendItem(filteredLowTemperatures, lowTemperatures[i]);
appendItem(filteredHighTemperatures, highTemperatures[i]);
}
}
}

I noticed that 1 is today’s weather myself. If the database has not been updated in a day it is yesterday’s weather. Looking at the Date column to find out what number to use is probably too complex. Just use 2.

cityList.length is equal to forecastsNumbers.length because of the way getColumn() works. However, I would call that a bad example to show the kids. I prefer your example.

Personally, I would go a step further.

var Tomorrows_Weather = 2;

function filter() {
  for (var i = 0; i < forecastsNumbers.length; i++) {
    if (forecastsNumbers[i] == Tomorrows_Weather) {
      appendItem(filteredCitiesNames, citiesNames[i]);
      appendItem(filteredConditions, conditions[i]);
      appendItem(filteredIcons, icons[i]);
      appendItem(filteredLowTemperatures, lowTemperatures[i]);
      appendItem(filteredHighTemperatures, highTemperatures[i]);
    }
  }
}

The reason for including the constant is that I have seen the idiom if(someArray[i] == 1){ in a bunch of posts with people asking what is going wrong. Since our kids look at this as a pattern they repeat it but they don’t understand what that == 1 is doing and just copy it. Naming it might help.

1 Like

Thank you so much. I love your solution. I will share it with my students. I agree with you that the example is not the best.

Thank you for answering. :slightly_smiling_face::slightly_smiling_face:

Thanks for bringing up this question. The usage of the forecastNumber column in the database seems to be a change from when this lesson was first written. I’ll update the example for this lesson! Thanks again :slight_smile:

2 Likes

You might also want to give the number a name as in the example. I have seen at least 5 posts with that exact mistake. I hadn’t realized until I saw this that is the source of the misunderstanding.

2 Likes

Ignore my bug submission. I submitted it before checking here and I see you mentioned you will change it. I kept wondering where my kids were getting 1 and why they were so insistent that something told them it had to be 1.

I am running into the uistring issue with a student as well. I have gone through the solution and can’t seem to figure out what the student did wrong. Here is the link:

https://studio.code.org/projects/applab/sEHyDU98q0yTKsop_1AXCkAOwj_eeBJD2QSbh5UBiDk

Your lists are empty. You must remove the “[” and “]” where you are initializing your lists. For example, line 4 should be as shown below (without the square braces).

var forecastNumbers = (getColumn(“Daily Weather”, “Forecast Number”));

Thank you so much, I didn’t even notice the
I appreciate the second set of eyes

I think someone mentioned this, but the daily weather dataset doesn’t seem to be updated in a timely manner. I have class in about 20 minutes, but the data set shows “today’s weather” (Forecast Number 1) as “Wed 4/6”. Today is 4/7. When does that data set get repoplulated? It would be great if it could be done overnight for those of us with early school start times. Thanks!