137k views
24 votes
Which of the following statements adds 21 days to the date in the dueDate variable below? var dueDate = new Date();?

a. dueDate = dueDate + 21;
b. dueDate = dueDate.getDate() + 21;
c. dueDate.setDate(21);
d. dueDate.setDate( dueDate.getDate() + 21 );

User Cookster
by
7.8k points

1 Answer

11 votes

Answer:

dueDate.setDate(dueDate.getDate() + 21);

Step-by-step explanation:

Given

The following declaration (in JavaScript):

var dueDate = new Date();

Required

Add 21 days to dueDate

Assume the date object is [mydate], the syntax to add [n] number of days to [mydate] in JavaScript is:

[mydate].setDate([mydate].getDate() + n);

In this question:

The variable is dueDate and the number of days is 21

Going by:

[mydate].setDate([mydate].getDate() + n);

The correct option is:

dueDate.setDate(dueDate.getDate() + 21);

User Brijesh Joshi
by
7.3k points