Aim of the puzzle:
Use the .reverse method .reverse()
to flip the order of the items in an array.
Walk through of solution:
The .reverse()
method flips the order of an array. To use it, attach .reverse()
to the array you want to reverse.
In the code that you start off with, there is an array, sentence
, a function, likeYodaSpeak
, and a function call, likeYodaSpeak(sentence)
. As it is, the likeYodaSpeak
function is incomplete - it only logs an array to the console. In this puzzle, you’ll complete it by adding the .reverse() method to the array
before it gets printed.
To complete the puzzle, attach .reverse()
to the array
that is on its own line inside the likeYodaSpeak
function declaration {}
. Now, when the code runs, the likeYodaSpeak
function will flip the order of the sentence
array.
Sample code solution:
const sentence = [' you are', ' learning JavaScript']
function likeYodaSpeak(array){
array.reverse()
console.log(array)
}
likeYodaSpeak(sentence);
Javascript Concepts: Array, String, Array Methods, Function Declaration, Function Call, console.log()
, .reverse()