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:

1 Like

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