Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /* Write a JavaScript function reverseString(str) that reverses string and returns it.
- Write JS program stringReverser.js that invokes your function with the sample input data below
- and prints the output at the console. */
- "use strict";
- function reverseString(str) {
- if (str.length > 1) {
- var str2 = '';
- for (var i = str.length - 1; i >= 0; i -= 1) {
- str2 += str[i];
- }
- return str2;
- } else return str;
- }
- console.log(reverseString('sample'));
- console.log(reverseString('softUni'));
- console.log(reverseString('java script'));
- console.log(reverseString('hello world'));
- console.log(reverseString('<html>sos</html>'));
- console.log(reverseString('C'));
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement