Search code examples
javascriptreddit

Passing data from a function to a Date function (let date = new Date ("Values"))


I try to solve this Reddit Daily Programming challenge and i just can't get past this hurdle. The task of the challenge is:

The program should take three arguments. The first will be a day, the second will be month, and the third will be year. Then, your program should compute the day of the week that date will fall on.

So I already figured out a function that solves the Weekday's problem. The task is, to create a program that takes 3 arguments. So I need to pass some values into the new Date variable that looks like this: ("December 25, 1987").

I created a function that takes 3 arguments: month, day, year. I also figured out how to store those values in the proper format to use it with the new Date variable.

The only problem is, I have no clue how to pass the output of my findOutDate(month, day, year) function to the new Date() variable.

It drives me crazy. Please help me. Also, maybe i got the challenge wrong and there is a much easier way to solve it.

// The program should take three arguments. The first will be a day, the second will be month, 
// and the third will be year. 
// Then, your program should compute the day of the week that date will fall on.

/**
 * Function takes in a Date object and returns the day of the week in a text format.
 */
function getWeekDay(date) {
  // Create an array containing each day, starting with Sunday.
  const weekdays = [
    "Sunday", 
    "Monday", 
    "Tuesday", 
    "Wednesday", 
    "Thursday", 
    "Friday",
    "Saturday"
  ];

  // Use the getDay() method to get the day.
  const day = date.getDay();

  // Return the element that corresponds to that index.
  return weekdays[day];
}

// The function that takes 3 arguments and stores them in a variable
function findOutDate(month, day, year) {
  // console.log(month + " " + day + ", " + year);
  let date = month + " " + day + ", " + year
}

// Finding out what day a specific date fell on.
let date = new Date('December 25, 1987');
let weekDay = getWeekDay(date);
console.log('Christmas Day in 1987 fell on a ' + weekDay);

Solution

  • function findOutDate(month, day, year) {
        // console.log(month + " " + day + ", " + year);
        let date = month + " " + day + ", " + year
        return date //return the date string
    }
    
    
    //Finding out what day a specific date fell on.
    let date = new Date(findOutDate()); //call the function returning the date string
    let weekDay = getWeekDay(date);
    

    Hope it helps