Last active
August 2, 2018 14:27
-
-
Save robgmerrill/d879f85fff5f5a79cb4315e0db5d7eec to your computer and use it in GitHub Desktop.
Reverse a String - Approach 1 - Built in Methods
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<!DOCTYPE html> | |
<html> | |
<head> | |
<meta charset="utf-8"> | |
<meta name="viewport" content="width=device-width"> | |
<title>JS Bin</title> | |
</head> | |
<body> | |
<script id="jsbin-javascript"> | |
// Reverse a String | |
// Challenge | |
// Reverse the provided string. | |
// You may need to turn the string into an array before you can reverse it. | |
// Your result must be a string. | |
// Tests | |
// 'car' -> 'rac' | |
// 'bar' -> 'rab' | |
// Approach 1 - // Using Built in Functions | |
// Tools | |
// split() method - splits a string into an array | |
// https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/split | |
// array reverse() method - reverses the order of an array | |
// https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/reverse | |
// join() method - joins array elements back into a string | |
// https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/join | |
function revString(str) { | |
// step 1 -> split string into an array | |
// var arrayToString = str.split(''); | |
// step 2 -> reverse array | |
// var reverseArray = arrayToString.reverse(); | |
// step 3 -> join array into string | |
// var reverseString = arrayToString.join(''); | |
// return reversed string | |
// return reverseString; | |
// all steps in one line | |
return str.split('').reverse().join(''); | |
} | |
// tests | |
console.log(revString('car')); | |
console.log(revString('bar')); | |
</script> | |
<script id="jsbin-source-javascript" type="text/javascript">// Reverse a String | |
// Challenge | |
// Reverse the provided string. | |
// You may need to turn the string into an array before you can reverse it. | |
// Your result must be a string. | |
// Tests | |
// 'car' -> 'rac' | |
// 'bar' -> 'rab' | |
// Approach 1 - // Using Built in Functions | |
// Tools | |
// split() method - splits a string into an array | |
// https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/split | |
// array reverse() method - reverses the order of an array | |
// https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/reverse | |
// join() method - joins array elements back into a string | |
// https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/join | |
function revString(str) { | |
// step 1 -> split string into an array | |
// var arrayToString = str.split(''); | |
// step 2 -> reverse array | |
// var reverseArray = arrayToString.reverse(); | |
// step 3 -> join array into string | |
// var reverseString = arrayToString.join(''); | |
// return reversed string | |
// return reverseString; | |
// all steps in one line | |
return str.split('').reverse().join(''); | |
} | |
// tests | |
console.log(revString('car')); | |
console.log(revString('bar'));</script></body> | |
</html> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Reverse a String | |
// Challenge | |
// Reverse the provided string. | |
// You may need to turn the string into an array before you can reverse it. | |
// Your result must be a string. | |
// Tests | |
// 'car' -> 'rac' | |
// 'bar' -> 'rab' | |
// Approach 1 - // Using Built in Functions | |
// Tools | |
// split() method - splits a string into an array | |
// https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/split | |
// array reverse() method - reverses the order of an array | |
// https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/reverse | |
// join() method - joins array elements back into a string | |
// https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/join | |
function revString(str) { | |
// step 1 -> split string into an array | |
// var arrayToString = str.split(''); | |
// step 2 -> reverse array | |
// var reverseArray = arrayToString.reverse(); | |
// step 3 -> join array into string | |
// var reverseString = arrayToString.join(''); | |
// return reversed string | |
// return reverseString; | |
// all steps in one line | |
return str.split('').reverse().join(''); | |
} | |
// tests | |
console.log(revString('car')); | |
console.log(revString('bar')); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment