Search This Blog

2023/10/11

Javascript Interview Question:Reverse Order the words in sentence

let str = "Today is Friday";
str = ' ' + str
var length = str.length;
var newStr=''

var last=length
for (let i = length - 1; i >= 0; i--) {
if(str[i]==' '){
newStr = newStr + ' ' + str.substring(i,last).trim()
last = i
}
}

console.log(newStr.trim())

Output:
Friday is Today


No comments:

Post a Comment