Created
December 31, 2019 11:21
-
-
Save Hellodhakad/263a75f0751597ad3b22bc33420c9b5f to your computer and use it in GitHub Desktop.
ReverseWord string js
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
var str = 'the sky is blue'; | |
var op = 'blue is sky the'; | |
function reverseWord(str){ | |
var subIndex = str.lastIndexOf(' '); | |
if(subIndex == -1){ | |
return str; | |
} | |
var lastWord = str.substring(subIndex+1, str.length); | |
var newStr = lastWord + ' ' + reverseWord(str.substring(0, subIndex)) | |
return newStr; | |
} | |
reverseWord(str) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment