-
-
Notifications
You must be signed in to change notification settings - Fork 44
/
double-char.js
90 lines (79 loc) · 2.38 KB
/
double-char.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
function doubleChar(str) {
// a place to store the doubled string
// iterate over each character in the string
// append the current character twice to the output string
// return the doubled string
}
function doubleChar(str) {
// a place to store the doubled string
let doubledString = '';
// iterate over each character in the string
for (let i = 0; i < str.length; i += 1) {
console.log(doubledString);
// append the current character twice to the output string
const currentCharacter = str[i];
doubledString += currentCharacter + currentCharacter;
}
// return the doubled string
return doubledString;
}
function doubleChar(str) {
let doubledString = '';
Array.from(str).forEach((character) => {
doubledString += character + character;
});
return doubledString;
}
function doubleChar(str) {
return Array
.from(str)
.reduce((doubledString, character) => {
return doubledString + character + character;
}, '');
}
function doubleChar(str) {
return str.split('')
.reduce((doubledString, character) => {
return doubledString + character + character;
}, '');
}
function doubleChar(str) {
return [...str]
.reduce((doubledString, character) => {
return doubledString + character + character;
}, '');
}
function doubleChar(str) {
let doubledString = '';
Array.from(str).forEach((character) => {
// doubledString += character.repeat(2);
doubledString = doubledString + character + character;
// doubledString = doubledString + character.repeat(2);
// doubledString = `${doubledString}${character}${character}`;
// doubledString += `${character}${character}`;
});
return doubledString;
}
function doubleChar(str) {
let doubledString = '';
for (const currentCharacter of str) {
doubledString += currentCharacter + currentCharacter;
}
return doubledString;
}
function doubleChar(str) {
return str.replace(/(.)/g, (c) => c.repeat(2));
}
function doubleChar(str) {
return str.replace(/(.)/g, '$1$1');
}
function doubleChar(str) {
return str.replace(/./g, '$&$&');
}
console.log(doubleChar("abcd"), "aabbccdd");
console.log(doubleChar("Adidas"), "AAddiiddaass");
console.log(doubleChar("1337"), "11333377");
console.log(doubleChar("illuminati"), "iilllluummiinnaattii");
console.log(doubleChar("123456"), "112233445566");
const result = doubleChar("%^&*(");
console.log(result, "%%^^&&**((");