Advertisement
dimipan80

Reverse String

Nov 12th, 2014
150
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /* Write a JavaScript function reverseString(str) that reverses string and returns it.
  2. Write JS program stringReverser.js that invokes your function with the sample input data below
  3. and prints the output at the console. */
  4.  
  5. "use strict";
  6.  
  7. function reverseString(str) {
  8.     if (str.length > 1) {
  9.         var str2 = '';
  10.         for (var i = str.length - 1; i >= 0; i -= 1) {
  11.             str2 += str[i];
  12.         }
  13.         return str2;
  14.     } else return str;
  15. }
  16.  
  17. console.log(reverseString('sample'));
  18. console.log(reverseString('softUni'));
  19. console.log(reverseString('java script'));
  20. console.log(reverseString('hello world'));
  21. console.log(reverseString('<html>sos</html>'));
  22. console.log(reverseString('C'));
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement