forked from CodeToExpress/dailycodebase
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsol4.js
More file actions
23 lines (18 loc) · 693 Bytes
/
sol4.js
File metadata and controls
23 lines (18 loc) · 693 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
/**
* @author MadhavBahlMD
* @date 21/12/2018
*/
// Step 1: Declare an empty string, say reverse = ''
// Step 2: Run a loop from 0 to length of string
// Step 3: For each letter of original string, concatenate reverse with it. (concatenate each element of string with reverse string, such that element gets concatenated before the current reverse string)
// Using basic string concatenation
function strRev (str) {
// Initialize an empty string "Reverse"
let reverse = '';
for (i=0; i<str.length; i++) {
// for each letter of original string, concatenate reverse with it
reverse = str[i] + reverse;
}
return reverse;
}
console.log(strRev('Hello!'));