Reverse a string without using any built-in string methods.

To reverse a string without using any built-in string methods, you can loop through the string and build a new string with the characters in reverse order.

Problem

Given a string, reverse the string without using any built-in string methods.
Example input:
"Hello, world!"
Example output:
"!dlrow ,olleH"

Solution

This solution is in JavaScript. Our A.I. can create solutions in multiple languages.
by kayakane
// Solution:
// The optimal solution is to use a for loop to iterate through the string, starting from the end and adding each character to a new string.
// This is optimal because it is O(n) time complexity and O(n) space complexity.

function reverseString(str) {
  let reversed = '';
  for (let i = str.length - 1; i >= 0; i--) {
    reversed += str[i];
  }
  return reversed;
}

console.log(reverseString('Hello, world!'));

A.I. Evaluation of the Solution

The candidate's solution is correct and demonstrates a level of completeness. The solution uses a for loop to iterate through the string, starting from the end and adding each character to a new string. This is optimal because it is O(n) time complexity and O(n) space complexity.

Evaluated at: 2022-11-21 10:16:17