Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //Write a function reverseHyphenString(string) that takes in a hyphenated string and returns a the hyphenated string reversed.
- //Added here because "reverse a string" is a classic interview question
- //Time complexity: O(n)
- //Space complexity: O(n)
- function reverseHyphenString(string){
- let choppedString = string.split("-") //Split the array at the hyphen
- let reversedString = []
- //Iterate backwards through the orignal word and add it to the reversed Array
- for(let i = choppedString.length - 1; i >= 0; i--){
- reversedString.push(choppedString[i])
- }
- reversedString = reversedString.join("-") //Join with a hyphen inserted. Thanks JS
- return reversedString;
- }
- console.log(reverseHyphenString("Go-to-the-store")) // => "store-the-to-Go"
Advertisement
Add Comment
Please, Sign In to add comment