将数据从函数传递到日期函数(设日期 = 新日期 ("Values"))
Passing data from a function to a Date function (let date = new Date ("Values"))
我试图解决这个 Reddit 每日编程挑战,但我就是无法越过这个障碍。挑战的任务是:
该程序应采用三个参数。第一个是日,第二个是月,第三个是年。
然后,您的程序应计算该日期将落在星期几。
所以我已经想出了一个解决 Weekday 问题的函数。任务是创建一个带有 3 个参数的程序。所以我需要将一些值传递到新的 Date 变量中,如下所示:("December 25, 1987")
。
我创建了一个带有 3 个参数的函数:month
、day
、year
。我还想出了如何以正确的格式存储这些值,以便将其与新的 Date 变量一起使用。
唯一的问题是,我不知道如何将 findOutDate(month, day, year)
函数的输出传递给 new Date()
变量。
这让我发疯。请帮我。另外,也许我把挑战弄错了,有更简单的方法来解决它。
// 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);
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);
希望对您有所帮助
我试图解决这个 Reddit 每日编程挑战,但我就是无法越过这个障碍。挑战的任务是:
该程序应采用三个参数。第一个是日,第二个是月,第三个是年。 然后,您的程序应计算该日期将落在星期几。
所以我已经想出了一个解决 Weekday 问题的函数。任务是创建一个带有 3 个参数的程序。所以我需要将一些值传递到新的 Date 变量中,如下所示:("December 25, 1987")
。
我创建了一个带有 3 个参数的函数:month
、day
、year
。我还想出了如何以正确的格式存储这些值,以便将其与新的 Date 变量一起使用。
唯一的问题是,我不知道如何将 findOutDate(month, day, year)
函数的输出传递给 new Date()
变量。
这让我发疯。请帮我。另外,也许我把挑战弄错了,有更简单的方法来解决它。
// 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);
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);
希望对您有所帮助