Skip to content

Instantly share code, notes, and snippets.

@jerolan
Created September 25, 2019 03:31
Show Gist options
  • Save jerolan/e4e9205b69016c89cb41860ec04a5d84 to your computer and use it in GitHub Desktop.
Save jerolan/e4e9205b69016c89cb41860ec04a5d84 to your computer and use it in GitHub Desktop.
/**
* First Reverse
* Have the function FirstReverse(str) take the str parameter
* being passed and return the string in reversed order.
* For example: if the input string is "Hello World and Coders"
* then your program should return the string sredoC dna dlroW olleH.
*
* Test Cases
* > firstReverse "Hello World and Coders"
* "sredoC dna dlroW olleH"
*
* > firstReverse "coderbyte"
* "etybredoc"
*/
function firstReverse(str) {
return str
.split("")
.reverse()
.join("");
}
@jerolan
Copy link
Author

jerolan commented Sep 25, 2019

Other solution could be

function firstReverse(str) {
  let mstr = "";
  for (let i = 1; i <= str.length; i++) {
    mstr += str[str.length - i];
  }
  return mstr;
}

@Sunboy005
Copy link

Another one is

function firstReverse(str) {
	if (typeof str != String) {str += ''}
	var str1 = [], str2 = [], i = 0;
	while (i < str.length) {
	  str1.push(str.substr(i++,1) );

	}
	for (i in str1) { 
	  str2.unshift(str1[i]); 
	}
	return String(str2.join('') ); 
}
   

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment