Code.org -- Searching in a database for the proper day and then adding it into a list

75 views Asked by At

I am working on a project for school and am almost finished with it. I need code.org to search through my database for Habits that occur on the day set. (Days of the week) then be put into a list and then displayed into an element called "TodaysHabits" Below is my code. Please note that I can only use functions in ES5 or approved by the code.org team (no push or pop)

Almost forgot these variables

var DateNum = new Date().getDay();
var DaysOfTheWeek = ["sunday","monday","tuesday","wednesday","thursday","friday","saturday"];
var Day = DaysOfTheWeek[DateNum];

onRecordEvent("Habit Data", function(records) {
    if (records.length == 0) {
      setText("TodaysHabits","Today's Habits" + "\n" + "No Habits to check");
    } else {
      readRecords("Habit Data", {Day:true}, function(records) { 
        if(records.length>0){
          for (var i =0; i < records.length; i++) {
            appendItem(TodayHabitList, record[i]);
          }
          setText("TodaysHabits","Today's Habits:"+"\n"+TodayHabitList.join("\n"));
        }
        else if(TodayHabitList.length==0) {
          setText("TodaysHabits","Today's Habits:"+"\n"+"No Habits scheduled today");
        }
      });
    }
},true);

The reason that there is a ",true" in the last line is to use the program on all existing habits in the code.org database.

I have rewrote this code at least 10 times each time getting slightly better than the last. I didn't save them (Horrible mistake) and don't have it for reference.

Here is the link to my project My Code.org project The CSV is "id,name,description,time,sunday,monday,tuesday,wednesday,thursday,friday,Saturday"

I forgot to add the part I'm inquiring about is Lines 142 - 158

1

There are 1 answers

0
Devom Brahmbhatt On

It's been about a month since this question... There was someone who answered this but they are not here anymore. Here are 2 ways to make the code work. You should create another variable, here it is named terms.

Terms = {Day:true}
Terms[Day] = true

Here is the code implemented

var DateNum = new Date().getDay();
var DaysOfTheWeek = ["sunday","monday","tuesday","wednesday","thursday","friday","saturday"];
var Day = DaysOfTheWeek[DateNum];
var Terms = {Day:true}
//var Terms[Day] = true
//Pick only one of these

onRecordEvent("Habit Data", function(records) {
    if (records.length == 0) {
      setText("TodaysHabits","Today's Habits" + "\n" + "No Habits to check");
    } else {
      readRecords("Habit Data", Terms, function(records) { 
        if(records.length>0){
          for (var i =0; i < records.length; i++) {
            appendItem(TodayHabitList, record[i]);
          }
          setText("TodaysHabits","Today's Habits:"+"\n"+TodayHabitList.join("\n"));
        }
        else if(TodayHabitList.length==0) {
          setText("TodaysHabits","Today's Habits:"+"\n"+"No Habits scheduled today");
        }
      });
    }
},true);

I don't know why or how this works but it worked for me so I'm not complaining. If you guys have any issues tag me and I'll see if I can clear it up for you. Cheers guys