Reverse a sentence in javascript without using inbuilt function

 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.

Reverse a sentence in javascript without using inbuilt function

asked Aug 15, 2017 at 10:28

Reverse a sentence in javascript without using inbuilt function

3

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

answered Nov 3, 2020 at 22:00

Reverse a sentence in javascript without using inbuilt function

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);

answered Aug 15, 2017 at 10:40

Reverse a sentence in javascript without using inbuilt function

yoav sarfatyyoav sarfaty

431 gold badge1 silver badge8 bronze badges

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

answered Aug 15, 2017 at 10:36

Reverse a sentence in javascript without using inbuilt function

marvel308marvel308

10.1k1 gold badge20 silver badges32 bronze badges

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))

answered Aug 15, 2017 at 10:32

Reverse a sentence in javascript without using inbuilt function

prasanthprasanth

21.6k4 gold badges27 silver badges50 bronze badges

2

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));

answered Aug 15, 2017 at 10:42

Reverse a sentence in javascript without using inbuilt function

Satyam PathakSatyam Pathak

6,1003 gold badges23 silver badges50 bronze badges

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

answered Apr 26, 2021 at 11:16

mohd jagirmohd jagir

711 silver badge3 bronze badges

I think using below way is simple and clean.

function reverse(str) {

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

}

answered Dec 12, 2021 at 10:50

Reverse a sentence in javascript without using inbuilt function

1

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);

answered Aug 15, 2017 at 10:32

Reverse a sentence in javascript without using inbuilt function

atiq1589atiq1589

2,1211 gold badge15 silver badges24 bronze badges

5

How do you reverse a sentence in JavaScript?

JavaScript Algorithm: Reverse Words.
let reverseWordArr = str.split(" ") Since we want to create another array but containing all the words in the string reversed, we will use the map method..
. map(word => word. split(""). ... .
let reverseWordArr = str. split(" "). ... .
return reverseWordArr.join(" "); Here is the full function:.

How can I reverse a string without any inbuilt function?

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);

How do I reverse a string without string builder?

Just use a single array, with two index variables, and swap the first and the last characters, then swap the second and the next to last characters, etc... You keep doing that until your two indexes meet in the middle.

Is there any reverse function in JavaScript?

JavaScript Array reverse() The reverse() method reverses the order of the elements in an array. The reverse() method overwrites the original array.