如何通过函数更新对象内的全局列表的全局值?

时间:2017-11-16 21:16:35

标签: javascript arrays list function object

我正在尝试使用JavaScript制作基于文本的日历。我现在坚持的是当我将项目添加到日历后搜索该项目。随意在firefox或任何你的开发人员使用它来查看我的意思。当我搜索一个值时,会输出一个空白屏幕而不是当天安排的值(更具体地说,是推送到该特定列表的项目。)我应该怎样做才能更新日历列表项目最简单的方法吗?

var userInput; // user's choice of 'e', 's', or 'q'
var enterInfo; // item that is entered into calender from user
var searchInfo; // item that is used to search calendar for entered events

var Calendar = { // calender of scheduled events of user
  monday: {
    morning: [],
    afternoon: [],
    evening: []
  },
  tuesday: {
    morning: [],
    afternoon: [],
    evening: []
  },
  wednesday: {
    morning: [],
    afternoon: [],
    evening: []
  },
  thursday: {
    morning: [],
    afternoon: [],
    evening: []
  },
  friday: {
    morning: [],
    afternoon: [],
    evening: []
  },
};

var addEventToCalendar = function(day, time) { // adds event from user to calender
  switch (day) {
    case "monday": // if day is monday
      switch (time) {
        case "morning":
          Calendar.monday.morning.push(info[2]);
          break;
        case "afternoon":
          Calendar.monday.afternoon.push(info[2]);
          break;
        case "evening":
          Calendar.monday.evening.push(info[2]);
          break;
      }
      break;
    case "tuesday": // if day is monday
      switch (time) {
        case "morning":
          Calendar.tuesday.morning.push(info[2]);
          break;
        case "afternoon":
          Calendar.tuesday.afternoon.push(info[2]);
          break;
        case "evening":
          Calendar.tuesday.evening.push(info[2]);
          break;
      }
      break;
    case "wednesday": // if day is monday
      switch (time) {
        case "morning":
          Calendar.wednesday.morning.push(info[2]);
          break;
        case "afternoon":
          Calendar.wednesday.afternoon.push(info[2]);
          break;
        case "evening":
          Calendar.wednesday.evening.push(info[2]);
          break;
      }
      break;
  }
}

var outputEvents = function(day, time) { // adds event from user to calender
  switch (day) {
    case "monday": // if day is monday
      switch (time) {
        case "morning":
          alert(Calendar.monday.morning);
          break;
        case "afternoon":
          alert(Calendar.monday.afternoon);
          break;
        case "evening":
          alert(Calendar.monday.evening);
          break;
      }
      break;
    case "tuesday": // if day is monday
      switch (time) {
        case "morning":
          alert(Calendar.tuesday.morning);
          break;
        case "afternoon":
          alert(Calendar.tuesday.afternoon);
          break;
        case "evening":
          alert(Calendar.tuesday.evening);
          break;
      }
      break;
    case "wednesday": // if day is monday
      switch (time) {
        case "morning":
          alert(Calendar.wednesday.morning);
          break;
        case "afternoon":
          alert(Calendar.wednesday.afternoon);
          break;
        case "evening":
          alert(Calendar.wednesday.evening);
          break;
      }
      break;
  }
}

var checkEnterInfo = function(info) { // checks if 'enterInfo' is valid
  info = info.split(", "); // splits info up to check if valid
  if (info.length != 3) { // checks if input from user has only 3 input values
    alert("Enter your information correctly, please.\n\n" +
      "Time, Day, Name (ex: morning, Monday, write letter)");
    mainProgram();
  } else if (isNaN(info[0] == false) || isNaN(info[1] == false) || isNaN(info[2]) == false) { // checks if all input values are strings
    alert("Please enter only alphabetic characters as your information.");
    mainProgram();
  } else if (info[0] != "morning" && info[0] != "afternoon" && info[0] != "evening") { // checks if time of day is valid
    alert("Please enter only one of the following for the time of day:\n\n" +
      "'Morning', 'Afternoon', or 'Evening'");
    mainProgram();
  } else if (info[1] != "monday" && info[1] != "tuesday" && info[1] != "wednesday" && info[1] != "thursday" && info[1] != "friday") { // checks if day from user is valid
    alert("Please enter only one of the following for the day of the event:\n\n" +
      "'Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday'");
    mainProgram();
  } else {
    addEventToCalendar(info[0], info[1]);
    mainProgram();
  }
}

var checkSearchInfo = function(info) {
  info = info.split(", "); // splits info to check if valid
  if (info.length != 2) { // checks if info does not have 2 input values to search for event
    alert("Please enter your information like so:\n\n" +
      "ex. Afternoon, Wednesday");
    mainProgram();
  } else if (isNaN(info[0]) == false || isNaN(info[1]) == false) { // checks if input values are strings
    alert("Please enter only alphabetic characters as your information.");
    mainProgram();
  } else if (info[0] != "morning" && info[0] != "afternoon" && info[0] != "evening") { // checks if time of day is valid
    alert("Please enter only one of the following for the time of day:\n\n" +
      "'Morning', 'Afternoon', or 'Evening'");
    mainProgram();
  } else if (info[1] != "monday" && info[1] != "tuesday" && info[1] != "wednesday" && info[1] != "thursday" && info[1] != "friday") { // checks if day from user is valid
    alert("Please enter only one of the following for the day of the event:\n\n" +
      "'Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday'");
    mainProgram();
  } else {
    outputEvents(info[1], info[0]);
  }
}
var mainProgram = function() { // main program function
  userInput = prompt("(E)nter an event, (S)earch, or (Q)uit: ").toLowerCase();

  if (userInput == 'q') { // user wants to quit
    alert("You have chosen to end the program.");
    return userInput;
    mainProgram();
  } else if (userInput == 's') { // search for event in calendar
    searchInfo = prompt("Enter time and day: ");
    checkSearchInfo(searchInfo);
  } else if (userInput == 'e') { // add event to calendar
    enterInfo = prompt("Enter time, day, and name: ");
    checkEnterInfo(enterInfo);
  } else { // user did not enter an available option
    alert("Choose from one of the options shown:" +
      "\n\n(E)nter an event, (S)earch, or (Q)uit: ");
    mainProgram();
  }
}

mainProgram();

1 个答案:

答案 0 :(得分:0)

您的函数addEventToCalendar无法访问info [2],您不能将其作为参数传递,并且它不在上限范围内。

当你调用函数addEventToCalendar时,你应该添加一个thrid参数并传递信息[2]。或者只传递一个参数信息,并通过函数声明中的info [0],info [1] et info [2]访问它。