const ex = "My favourite game";
function wordReverse(str) {
var newStr = "",
strLength = str.length;
for (var i=0; i<strLength; i++) {
newStr[i] = str[strLength - 1 - i];
}
return newStr;
}
function wordsReverse(str) {
str += " ";
var wordsReverseArray = [],
wordStr = "";
for(var i=0; i<str.length; i++) {
if(str[i] !== " ") {
wordStr += str[i];
} else {
wordsReverseArray.push(wordReverse(wordStr));
wordStr = "";
}
}
return wordsReverseArray;
}
console.log(wordsReverse(ex)); // Array(3) [ "", "", "" ]
wordStr += str[i];
newStr[i] = str[strLength - 1 - i];
newStr += str[strLength - 1 - i];
const ex = "My favourite game";
function wordReverse(str) {
var newStr = '',
strLength = str.length;
for (var i = 0; i < strLength; i++) {
newStr += str[strLength - 1 - i];
}
return newStr;
}
function wordsReverse(str) {
str += " ";
var wordsReverseArray = [],
wordStr = "";
for(var i=0; i<str.length; i++) {
if(str[i] !== " ") {
wordStr += str[i];
} else {
wordsReverseArray.push(wordReverse(wordStr));
wordStr = "";
}
}
return wordsReverseArray.join(' ');
}
console.log(wordsReverse(ex)); // "yM etiruovaf emag"
reverseString('hello, world'); //> "dlrow ,olleh"
function reverseString(string) {
return string.split('').reverse().join('');
}
const ex = "My favourite game";
console.log(ex.split(' ').map((item) => { return item.split('').reverse().join(''); }).join(' '));
var ex = "My favourite game";
console.log(ex.split(' ').map(function(item) { return item.split('').reverse().join(''); }).join(' '));