0

I've been working on a function for a web page that should display information from a JSON object depending upon two attributes, the attendance day and start time. The JSON and function are as follows is as follows:

var courses = [{
  "dept": "CSC",
  "id": "3380",
  "instructor": "Douglas",
  "Location": "012 Lockett",
  "Start_Time": "8:00",
  "End_Time": "9:30",
  "Attendance": "Monday"
}, {
  "dept": "CSC",
  "id": "3380",
  "instructor": "Douglas",
  "Location": "012 Lockett",
  "Start_Time": "10:30",
  "End_Time": "11:30",
  "Attendance": "Monday"
}, {
  "dept": "CSC",
  "id": "3380",
  "instructor": "Douglas",
  "Location": "012 Lockett",
  "Start_Time": "12:30",
  "End_Time": "1:30",
  "Attendance": "Wednesday"
}];

function plotCourse() {
  var i;
  for (i = 0; i < courses.length; i++) {
    var course = courses[i];
    console.log(course);
    if (course.Attendance == "Monday" && course.Start_Time == "10:30") {
      alert(course.Attendance);
      alert(course.Start_Time);
    }
  }
}

I'm using alert() for testing purposes. However, I don't seem to get a returned value, and I'm a bit stumped and what may be going on.

Thanks in advance.

2
  • 4
    call plotCourse() Commented Nov 28, 2016 at 0:38
  • 1
    this is just a hunch, and I may be wrong, but in your example you don't actually call the plotCourse() function. You just define it. Commented Nov 28, 2016 at 0:39

5 Answers 5

2

you forget to call it: plotCourse();, or use IIFE instead. please check this one

var courses = [{
  "dept": "CSC",
  "id": "3380",
  "instructor": "Douglas",
  "Location": "012 Lockett",
  "Start_Time": "8:00",
  "End_Time": "9:30",
  "Attendance": "Monday"
}, {
  "dept": "CSC",
  "id": "3380",
  "instructor": "Douglas",
  "Location": "012 Lockett",
  "Start_Time": "10:30",
  "End_Time": "11:30",
  "Attendance": "Monday"
}, {
  "dept": "CSC",
  "id": "3380",
  "instructor": "Douglas",
  "Location": "012 Lockett",
  "Start_Time": "12:30",
  "End_Time": "1:30",
  "Attendance": "Wednesday"
}];

(function() {
  var i;
  for (i = 0; i < courses.length; i++) {
    var course = courses[i];
    console.log(course);
    if (course.Attendance == "Monday" && course.Start_Time == "10:30") {
      alert(course.Attendance);
      alert(course.Start_Time);
    }
  }
})(); /* IIFE*/

Sign up to request clarification or add additional context in comments.

Comments

1

Hope you were calling the function.

Working here:

var courses = [{
  "dept": "CSC",
  "id": "3380",
  "instructor": "Douglas",
  "Location": "012 Lockett",
  "Start_Time": "8:00",
  "End_Time": "9:30",
  "Attendance": "Monday"
}, {
  "dept": "CSC",
  "id": "3380",
  "instructor": "Douglas",
  "Location": "012 Lockett",
  "Start_Time": "10:30",
  "End_Time": "11:30",
  "Attendance": "Monday"
}, {
  "dept": "CSC",
  "id": "3380",
  "instructor": "Douglas",
  "Location": "012 Lockett",
  "Start_Time": "12:30",
  "End_Time": "1:30",
  "Attendance": "Wednesday"
}];

function plotCourse() {
  var i;
  for (i = 0; i < courses.length; i++) {
    var course = courses[i];
    console.log(course);
    if (course.Attendance == "Monday" && course.Start_Time == "10:30") {
      console.log(course.Attendance);
      console.log(course.Start_Time);
    }
  }
}
plotCourse(); //Involed the function

Comments

1

You forgot to call the plotCourse() function.

var courses = [{
  "dept": "CSC",
  "id": "3380",
  "instructor": "Douglas",
  "Location": "012 Lockett",
  "Start_Time": "8:00",
  "End_Time": "9:30",
  "Attendance": "Monday"
}, {
  "dept": "CSC",
  "id": "3380",
  "instructor": "Douglas",
  "Location": "012 Lockett",
  "Start_Time": "10:30",
  "End_Time": "11:30",
  "Attendance": "Monday"
}, {
  "dept": "CSC",
  "id": "3380",
  "instructor": "Douglas",
  "Location": "012 Lockett",
  "Start_Time": "12:30",
  "End_Time": "1:30",
  "Attendance": "Wednesday"
}];

function plotCourse() {
  var i;
  for (i = 0; i < courses.length; i++) {
    var course = courses[i];
    console.log(course);
    if (course.Attendance == "Monday" && course.Start_Time == "10:30") {
      alert(course.Attendance);
      alert(course.Start_Time);
    }
  }
}

plotCourse();

Comments

0

the example you proposed was not the called function. iniltre I made a small correction to the FOR loop.. =)

function plotCourse() {
  for (var i = 0; courses[i]; i++) { // Control if the element exists
    var course = courses[i];
    console.log(course);
    if (course.Attendance == "Monday" && course.Start_Time == "10:30") {
      alert(course.Attendance);
      alert(course.Start_Time);
    }
  }
}

plotCourse(); // launch the function!

Comments

0

As others have already stated, you need to call the function. It's working for me when I call it.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.