Reverse the order of words in a string

Given a string, this program will reverse the order of the words.

Problem

Given a string, reverse the order of the words.
Input:
"The quick brown fox jumps over the lazy dog"
Output:
"dog lazy the over jumps fox brown quick The"

Solution

This solution is in JavaScript. Our A.I. can create solutions in multiple languages.
by kayakane
// Solution:
// The optimal solution is to split the string into an array of words, reverse the array, and then join the array back into a string.
// This is optimal because it is a linear time solution.

function reverseWords(str) {
  return str.split(' ').reverse().join(' ');
}

console.log(reverseWords("The quick brown fox jumps over the lazy dog"));

A.I. Evaluation of the Solution

The candidate's solution is correct and demonstrates a level of completeness. The candidate has correctly identified that the optimal solution is to split the string into an array of words, reverse the array, and then join the array back into a string. This is a linear time solution.

Evaluated at: 2022-11-21 02:16:43