Javascript Developer JSE 40 01 · Free Practice Question Easy
Question 15
Q107 - Data Types
In the daysOfWeek variable,
we place an array with names of the days of the week.
To reverse the order of the array elements, we should call:
-
A
- daysOfWeek.reverse();
-
B
- daysOfWeek.order(-1);
-
C
- daysOfWeek = reverse(daysOfWeek);
-
D
- daysOfWeek.invert();
Reveal correct answer
Correct answer: A
Explanation
Topic: array.reverse()
Try it yourself:
- let daysOfWeek = ["Monday", "Wednesday", "Friday"];
- daysOfWeek.reverse();
- console.log(daysOfWeek) // [ "Friday", "Wednesday", "Monday" ]
Explanation:
The reverse() method reverses the order of the elements in an array.
https://www.w3schools.com/jsref/jsref_reverse.asp
Q107 (Please refer to this number, if you want to write me about this question.)
A. Calling the reverse() method on an array in JavaScript will reverse the order of its elements. This is the correct method to use when you want to reverse the order of the elements in an array like daysOfWeek.
B. There is no order() method in JavaScript arrays that takes a parameter to reverse the order of elements. This choice is incorrect as it does not provide a valid method to reverse the order of the elements in the daysOfWeek array.
C. Assigning the result of calling a non-existent reverse() function on the daysOfWeek array will not reverse the order of the elements. This choice is incorrect as it does not provide a valid method to reverse the order of the elements in the daysOfWeek array.
D. There is no invert() method in JavaScript for arrays. This choice is incorrect as it does not provide a valid method to reverse the order of the elements in the daysOfWeek array.
Discussion
Think the marked answer is wrong, or have a better explanation? Share it below — comments appear after review.
