Skip to content

Commit e72bff4

Browse files
committed
added new copy of string revers
1 parent 37bd69d commit e72bff4

File tree

1 file changed

+53
-0
lines changed

1 file changed

+53
-0
lines changed
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
// string reversing challenge
2+
3+
/* Given a string of text, write
4+
an algorithm that returns the text received in a reversed format. */
5+
6+
//code Implementation
7+
// 1.first method- chaining built-in method
8+
9+
/*
10+
var text = "algorithm";
11+
12+
function reverseString(text) {
13+
return text.split("").reverse().join("")
14+
15+
}
16+
*/
17+
18+
function reverseString(str) {
19+
// Step 1. Use the split() method to return a new array
20+
var splitString = str.split(""); // var splitString = "hello".split("");
21+
// ["h", "e", "l", "l", "o"]
22+
23+
// Step 2. Use the reverse() method to reverse the new created array
24+
var reverseArray = splitString.reverse(); // var reverseArray = ["h", "e", "l", "l", "o"].reverse();
25+
// ["o", "l", "l", "e", "h"]
26+
27+
// Step 3. Use the join() method to join all elements of the array into a string
28+
var joinArray = reverseArray.join(""); // var joinArray = ["o", "l", "l", "e", "h"].join("");
29+
// "olleh"
30+
31+
//Step 4. Return the reversed string
32+
return joinArray; // "olleh"
33+
34+
}
35+
//console.log(reverseString("hello"));
36+
37+
// reverseString("hello");
38+
39+
40+
// 2. method: using for LOOP
41+
42+
function reversestring(text){
43+
44+
let result = "";
45+
46+
for(let i=text.length-1; i>=0; i--){
47+
result= result+text[i];
48+
}
49+
50+
return result
51+
}
52+
53+
console.log(reversestring("machine") ) // print the result

0 commit comments

Comments
 (0)