admin管理员组文章数量:1400116
function reverse1(str){
var a = "";
for(var i = 0; i <= str.length/2; i++){
a = str[i];
str[i] = str[str.length-i-1];
str[str.length-i-1] = a;
}
return str;
}
var str = "abcdef";
reverse1(str);
I want to reverse a string without using any inbuilt function, and I want it to change the original string itself but it doesn't work well. The language is Javascript.
function reverse1(str){
var a = "";
for(var i = 0; i <= str.length/2; i++){
a = str[i];
str[i] = str[str.length-i-1];
str[str.length-i-1] = a;
}
return str;
}
var str = "abcdef";
reverse1(str);
I want to reverse a string without using any inbuilt function, and I want it to change the original string itself but it doesn't work well. The language is Javascript.
Share Improve this question edited Aug 15, 2017 at 12:31 Tobias Geiselmann 2,5162 gold badges27 silver badges41 bronze badges asked Aug 15, 2017 at 10:28 田小强田小强 171 gold badge1 silver badge4 bronze badges 3- 1 You can't change original string. Strings are immutable – Yury Tarabanko Commented Aug 15, 2017 at 10:31
- 1 Strings in Javascript are immutable, so you can't modify the original string – Lennholm Commented Aug 15, 2017 at 10:31
- Possible duplicate of How do you reverse a string in place in JavaScript? – user47589 Commented Aug 15, 2017 at 13:15
9 Answers
Reset to default 6Here is the simplest way to do without using any javascript inbuilt function.
function reverse1(str){
let r = "";
for(let i = str.length-1; i >= 0; i--){
r += str[i];
}
return r;
}
console.log(reverse1("javascript"))
Create a new string and add all the chars from the original string to it backwards:
function reverse1(str){
var r = "";
for(var i = str.length - 1; i >= 0; i--){
r += str.charAt(i);
}
return r;
}
Then just say:
str = reverse1(str);
Javascript strings are immutable, you cannot simply replace a character with another one
function reverse1(str){
var len = str.length, result = "";
for(var i = 0; i <= len-1; i++){
result = result + str[len-i-1];
}
return result;
}
var str = "abcdef";
str = reverse1(str);
console.log(str);
You can always create a new string and return it though
Reverse the forloop
iteration From high to low i--
used to decrement the value of i
function reverse1(str) {
str = str.trim();
var res ="";
for(var i = str.length-1; i >= 0; i--){
res +=str[i];
}
return res;
}
var str = "abcdef";
console.log(reverse1(str))
Well if you don't want to use the inbuilt functions here you go
var string = 'hello';
function reverse(str) {
if(!str.trim() || 'string' !== typeof str) {
return;
}
var length=str.length;
s='';
while(length > 0) {
length--;
s+= str[l];
}
return s;
}
console.log(reverse(string));
const reverseString = (str = null) => {
let newStr = [];
let string = "";
let reverseStr = "";
for (i = 0; i < str.length; i++) {
if (str[i] == " ") {
newStr.push(string);
string = "";
} else {
string += str[i];
}
}
if (string) {
newStr.push(string);
}
for (i = newStr.length - 1; i >= 0; i--) {
reverseStr += newStr[i] + " ";
}
return reverseStr;
};
let val = reverseString("My name is mohd jagir");
console.log(val);
//output will be jagir mohd is name My
I think using below way is simple and clean.
function reverse(str) {
return str.split("").reduce((a,b)=> a = b + a ,"")
}
i think this will be easy way to implement without using any inbuild function,you can reverse string also if you unment the mented code
const restring = "abcdf ghj";
// console.log(restring.length)
let temp = "";
// let valSplit = restring.split(" ");
for(let i = restring.length-1;i >= 0;i--){
temp += (restring[i]) ;
}
console.log(temp)
I think this question better answer is here. why char can't be change in a position.
Duplicate
function reverse(s){
return s.split("").reverse().join("");
}
var originalString = "ABCD";
console.log(reverse(originalString));
console.log(originalString);
originalString = reverse(originalString);
console.log(originalString);
本文标签: Reverse a string in javascript without using any inbuilt functionStack Overflow
版权声明:本文标题:Reverse a string in javascript without using any inbuilt function - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744158495a2593223.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论