Skip to content
TheChatArchiveThe Chat Archive
Claudeclaude-2.1· AnthropicHugging Face Arena Human Preference 55K

Write a one line JS function that reverses an array without using any built in methods

From a Chatbot Arena battleThe same person sent this prompt to two anonymous models at once. Compare it with gpt-4-1106-preview’s answer.
  1. User

    Write a one line JS function that reverses an array without using any built in methods

  2. Claude Assistant

    Here is a one line function to reverse an array without using built-in methods:

    const reverse = arr => arr.reduceRight((prev, curr) => [curr, ...prev], []);
    

    To break this down:

    • reduceRight iterates the array in reverse order
    • The prev argument accumulates the reversed array
    • The curr argument is the current element
    • On each iteration, we prepend curr to prev to build up a reversed copy
    • The empty array [] is the initial value for prev

    So this traverses the array backwards, and builds up a new reversed array iteratively.

Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.