2

I am making a mini calendar. I am trying to pass the appropriate values into the view section of the object. But I keep on getting undefined in the view section. What am I doing wrong?

var date = {  

  monthsArray: ["January", "February","March","April","May","June","July","August", "September", "October", "November","December" ],
  init: function() {
    var fullDate = new Date();
    this.dateParsed(fullDate);
    this.view();
  },
  stringifyMonth: function(monthNumber) {
    var monthName = this.monthsArray[monthNumber]
    return monthName
  },
  dateParsed: function(fullDate){
   var fullDate = new Date();
   var monthNumber = fullDate.getMonth();
   var dayNumber = fullDate.getDate();
   var calanderYear = fullDate.getFullYear();
   this.stringifyMonth(monthNumber)
   return calanderYear
  },
  view: function(monthName){

    console.log(this.stringifyMonth())   ///  undefined 


  }

}.init()
2
  • Return this from init? Commented Oct 18, 2015 at 6:01
  • @torazaburo That won't make any difference in this case as the date object is not used from outside. Commented Oct 18, 2015 at 6:12

2 Answers 2

3

Two things:

  1. Pass current month to the view function. You can get the month from date object using getMonth()
  2. Pass the same month to the stringifyMonth from view

See the highlighted changes and comments

var date = {

  monthsArray: ["January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"],
  init: function() {
    var fullDate = new Date();
    this.dateParsed(fullDate);

    
    // Get the current month from date object and pass to the view()
    this.view(fullDate.getMonth()); // <-----
  },
  stringifyMonth: function(monthNumber) {
    var monthName = this.monthsArray[monthNumber]
    return monthName
  },
  dateParsed: function(fullDate) {
    var fullDate = new Date();
    var monthNumber = fullDate.getMonth();
    var dayNumber = fullDate.getDate();
    var calanderYear = fullDate.getFullYear();
    this.stringifyMonth(monthNumber)
    return calanderYear
  },
  view: function(monthName) {
    
    // Pass the monthName to the stringifyMonth()
    console.log(this.stringifyMonth(monthName)); // <-----
  }
}.init();

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

2 Comments

Can you explain why it didn't work the way I had it? Did it have to do with scope?
@YoniMessing Since you haven't passed parameters, their value is undefined so the error is thrown. If you don't want to pass them as parameter and access directly you need to use this.varName
2

stringifyMonth expects a parameter, but you are not passing one. thus, null is passed as a parameter, and then you are trying to access this.monthsArray[null], which is undefined.

Try passing a parameter like 1 and you will see that it print February.

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.