Last active
April 28, 2022 06:30
-
-
Save Shaxadhere/cc3067a103d42024a170ac0ce57e0f2f to your computer and use it in GitHub Desktop.
Reverse words in sentence in JavaScript without using JS pre built functions
This file contains hidden or 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
let string = "NodeJS is single threaded"; | |
let array = []; | |
let ress = ""; | |
let reversed = ""; | |
let tempVal = ""; | |
let str = string ; | |
for (let index = 0; index < str.length; index++) { | |
if (str[index] != " " || (index +1) === (str.length)) { | |
tempVal += str[index]; | |
if(index+1===str.length){ | |
array.push(tempVal); | |
let reversed = ""; | |
for (let value of tempVal) { | |
reversed = value + reversed; | |
} | |
ress += " " + reversed; | |
tempVal = ""; | |
} | |
} else { | |
array.push(tempVal); | |
let reversed = ""; | |
for (let value of tempVal) { | |
reversed = value + reversed; | |
} | |
ress += " " + reversed; | |
tempVal = ""; | |
} | |
reversed = str[index] + reversed; | |
} | |
console.log("result: ",ress); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment