-1

i am doing a javascript assessment concerning the class Date Here is the assessment: "Write the body of the nextWeek(date) function that returns a date 7 days after the date given in input" date is always a defined Date object.

So i wrote that code below:

function nextWeek(date){
var today=new Date();
var nextweek=new Date(today.getFullYear(),today.getMonth(),today.getDate()+7);
return nextweek;
}
var d=new Date();
console.log(d);
console.log(nextWeek(d));

And the result is :

"2021-04-25T15:02:16.234Z"

"2021-05-01T22:00:00.000Z"

for me it is correct because there is one week(7 days) long between "2021-04-25T15:02:16.234Z" and "2021-05-01T22:00:00.000Z"

But they told me that my code is wrong,i don't know what is wrong with it, Do you have any idea of what's wrong in that code above?

bassey
  • 13
  • 2
  • 2
    Depends on why they think it's wrong. – Dave Newton Apr 25 '21 at 15:09
  • 1
    It isn't *wrong*, but it probably just doesn't do it the way they think it should be done. There are some issues: 1. Lack of indentation. 2. Using `var` (in modern JavaScript, always use `const` or `let`). 3. Creating a date to assign to `today` and then immediately overwriting it with a different value -- there's no reason for the first date you're creating. But the way you're moving forward seven days is just fine. Perhaps they don't understand that `Date` handles wrapping to the next month automatically. And kudos for returning a new `Date` rather than modifying the one you're given. – T.J. Crowder Apr 25 '21 at 15:10
  • they said the solution doesn't work for the current date and another date and also they said my solution does not have to modify the input Date – bassey Apr 25 '21 at 15:14
  • 1
    @bassey put your above comment in the actual question where it should have been all along, we aren't telepaths that knew what they thought was wrong – Dexygen Apr 25 '21 at 15:27
  • it is ok,the solutions below are fine – bassey Apr 25 '21 at 15:34

2 Answers2

-1

That may be a one liner:

const now = new Date();
console.log(`now: ${now.toLocaleString()}`);
console.log(`a week from now: ${nextWeek4SomeDate(now).toLocaleString()}`);
console.log(`check: now is still now? ${now.toLocaleString()}`);

function nextWeek4SomeDate(date) { 
  return new Date(new Date(date).setDate(date.getDate() + 7));
//        |       |              └─ returns adjusted date as a number value
//        |       |                 i.e. the number of milliseconds between 
//        |       |                 1 January 1970 00:00:00 UTC and the given date
//        |       └─ create a new Date from the [date], so the original date is not modified          
//        └─ the number value from setDate should be reconverted to a real Date
}
KooiInc
  • 104,388
  • 28
  • 131
  • 164
  • Thanks,it works but i need to use a function rather than a const ,otherwise it is a good solution – bassey Apr 25 '21 at 15:36
  • It *is* a (n arrow) function (expression), but hey, now it's a [function declaration](https://stackoverflow.com/questions/336859/var-functionname-function-vs-function-functionname) – KooiInc Apr 25 '21 at 15:55
-2
function nextWeek(date){
  var today=new Date(); // Remove this
  var nextweek=new Date(today.getFullYear(),today.getMonth(),today.getDate()+7); // Change today -> date
  return nextweek;
}
var d=new Date();
console.log(d);
console.log(nextWeek(d)); 

// Simple way of doing it.
function nextWeek(date) {
  date.setDate(date.getDate() + 7);
  return date;
}
Jr Mendoza
  • 39
  • 4
  • There are two function declarations in your answer. The last one is used (because it's hoisted). Furthermore, that function modifies the original date. – KooiInc Apr 25 '21 at 16:02
  • @KooiInc the second function was just an enhancement / refactored form of his existing code. It doesn't necessarily to be like that. I added a comment above it //Simple way of doing it – Jr Mendoza Apr 25 '21 at 16:08