-1
 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.

Tobias Geiselmann
  • 2,139
  • 2
  • 23
  • 36
田小强
  • 17
  • 1
  • 1
  • 4
  • 1
    You can't change original string. Strings are immutable – Yury Tarabanko Aug 15 '17 at 10:31
  • 1
    Strings in Javascript are immutable, so you can't modify the original string – Lennholm Aug 15 '17 at 10:31
  • Possible duplicate of [How do you reverse a string in place in JavaScript?](https://stackoverflow.com/questions/958908/how-do-you-reverse-a-string-in-place-in-javascript) –  Aug 15 '17 at 13:15

9 Answers9

6

Here 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"))
0

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))
prasanth
  • 22,145
  • 4
  • 29
  • 53
  • OP said "without using any inbuild function" – taha Aug 15 '17 at 10:33
  • 2
    Not only did OP specifically ask for no built-in functions but also that the original string should be modified, which is impossible. The only proper answer to this question is "What you're asking for is not possible in Javascript" – Lennholm Aug 15 '17 at 11:17
0

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

marvel308
  • 10,288
  • 1
  • 21
  • 32
0

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);
yoav sarfaty
  • 43
  • 1
  • 1
  • 8
0

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));
Satyam Pathak
  • 6,612
  • 3
  • 25
  • 52
0
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
mohd jagir
  • 71
  • 1
  • 3
0

I think using below way is simple and clean.

function reverse(str) {

return str.split("").reduce((a,b)=> a = b + a ,"")

}

  • 1
    The OP asked for a solution `without using any inbuilt function`, so this does not seem to be an answer to his question. – Bert Blommers Dec 12 '21 at 10:55
-1

i think this will be easy way to implement without using any inbuild function,you can reverse string also if you uncomment the commented 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)
  • This is quite an old question and your answer is the same as some of the already existing answers – Moudi Dec 19 '22 at 07:43
-4

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);
atiq1589
  • 2,227
  • 1
  • 16
  • 24