Javascript Algorithms: Reversing a String

JavaScript Algorithms: Reversing a String

As I prepare for Javascript development interviews I've been reviewing various online articles and resources about different algorithms I'll likely get asked about. Turns out that these things are actually pretty fun exercises and while I was able to do some of them all by my lonesome, others required some documentation lookups and Googling. Like any development problem there are likely multiple good solutions but the ones I present here are probably the most direct and obvious - feel free to solve differently and generate your own solution.

Where possible I'll cite my source(s) and give credit to any resources that helped me solve this algorithm.

REVERSING A STRING

Resources:

https://scotch.io/courses/the-ultimate-guide-to-javascript-algorithms/string-reversal

Write a solution in Javascript where, if I give you a string of text your solution will return that same string but in reverse order. For example if I provide, "abcd" your solution will return "dcba".

USAGE: Copy and paste the code samples below into the Javascript console in your browser, then simply hit return on your keyboard to see the code in action.

//REVERSE A STRING OF TEXT
function reverseString(text) {
  return text
    .split("") //breaks up the string into an array
    .reverse() //reverses the order of an array
    .join(); //reassembles the parts to return
}
console.log(reverseString("algorithm"));

function reverseString2(text) {
  let result = ""; //set the result variable

  for (let char of text) {
    //loop through the submitted text string
    result = char + result; //concatenate current iterator in front of current result set
  }
  return result;
}
console.log(reverseString2("algorithm"));


Link your website to this page! Copy and paste the URL below:
http://www.cfsnap.com/javascript/javascript-algorithms-reversing-a-string/
Copyright 2019. All Rights Reserved.