JavaScript Odwróć ciąg
function reverseString(s){
return s.split("").reverse().join("");
}
reverseString("Hello");//"olleH"
Grepper
function reverseString(s){
return s.split("").reverse().join("");
}
reverseString("Hello");//"olleH"
//more compact way:
"my example text".split("").reverse().join("");
return str.split("").reverse().join("");
function reverseString(str) {
var newString = "";
for (var i = str.length - 1; i >= 0; i--) {
newString += str[i];
}
return newString;
}
console.log(reverseString('welcome'));
const reverseStr = (str)=>{
if(!str || 2 > str.length) {
return str
}
let reversedStr = '';
for (let index = str.length - 1; index >= 0; index--) {
reversedStr += str[index];
}
return reversedStr
}
console.log(reverseStr('football'));
// program to reverse a string
function reverseString(str) {
// return a new array of strings
const arrayStrings = str.split("");
// reverse the new created array elements
const reverseArray = arrayStrings.reverse();
// join all elements of the array into a string
const joinArray = reverseArray.join("");
// return the reversed string
return joinArray;
}
// take input from the user
const string = prompt('Enter a string: ');
const result = reverseString(string);
console.log(result);