Question Anastasiia in lesson «Spread syntax (unpacking arguments)», course «JS: Functions»
Hi there, you have following task for spread operator
Implement and export as default a function that converts dates into an array of human-readable English text. Each date is represented by an array [2001, 10, 18], where the first element is the year, the second is the month, and the third is the day.
And you have following example:
convert([1993, 3, 24]);
// ['Sat Apr 24 1993']
And I'm confused cos 3rd month is March not April. Did I miss something in the task description where I need to move it one month forward?
Anastasiia,
months in JS are zero-based indexed. This means that January is represented by 0, February 1, and so on. Therefore, when counting from the begging of year, April corresponds to the third index (3rd position) in JS. Let's take a look at another example , we're are going to take the current day and obtain the date, month, and year. Here is the output:
const today = new Date(); // assume it's Jun 30, 2023
const [month, day, year] = [
today.getMonth(),
today.getDate(),
today.getFullYear(),
];
console.log(month); // 5
console.log(day); // 30
console.log(year); // 2023
Thanks for explanation.
I was using this code new Date([1993, 3, 24])
and it was leading to the different result.
Thanks to your explanation I realised the issue:)
Use Hexlet to the fullest extent!
- Ask questions about the lesson
- Test your knowledge in quizzes
- Practice in your browser
- Track your progress
Sign up or sign in