​Case-Based Critical Thinking Questions​Case 9-1Melissa, a computer science engineering student, is learning the basics of programming by exploring arrays. She learns about different array methods. She wishes to learn more on inserting and deleting array items. She creates monthName array to extract only the three spring months-March, April, and May-from a calendar.​Melissa wants to insert abbreviations of spring months into the monthName array. Which of the following methods should she use?
A. ​monthName.reverse(5, 3, "Mar", "Apr", "May");B. ​monthName.sort(2, 3, "Mar", "Apr", "May");C. ​monthName.join(5, 3, "Mar", "Apr", "May");D. ​monthName.splice(2, 3, "Mar", "Apr", "May");

Respuesta :

Answer:

Option D:  monthName.splice(2, 3, "Mar", "Apr", "May");

Explanation:

splice is a JavaScript array method which can be used to insert or remove item(s).

It has general syntax:  splice( i, n , item1, item2, item3, .... , itemX)

  • i : index position of the array where the item should be inserted or removed
  • n: number of item(s) should be removed starting from index-i
  • item1,item2,....itemX :  item(s) to be inserted

If we have an original monthName array with elements:

["January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"]

monthName.splice(2, 3, "Mar", "Apr", "May")  will

  • remove 3 items from index position-2 ("March", "April" and "May")
  • and insert three new items, "Mar", "Apr" and "May".

New array:

["January", "February", "Mar", "Apr", "May", "June", "July", "August", "September", "October", "November", "December"]