Initial data commit
This commit is contained in:
parent
72d218235f
commit
f23f22d71c
199087 changed files with 3378941 additions and 0 deletions
9
Task/Reverse-a-string/JavaScript/reverse-a-string-1.js
Normal file
9
Task/Reverse-a-string/JavaScript/reverse-a-string-1.js
Normal file
|
|
@ -0,0 +1,9 @@
|
|||
example = 'Tux 🐧 penguin';
|
||||
|
||||
// array expansion operator
|
||||
[...example].reverse().join('') // 'niugnep 🐧 xuT'
|
||||
// split regexp separator with Unicode mode
|
||||
example.split(/(?:)/u).reverse().join('') // 'niugnep 🐧 xuT'
|
||||
|
||||
// do not use
|
||||
example.split('').reverse().join(''); // 'niugnep \udc27\ud83d xuT'
|
||||
11
Task/Reverse-a-string/JavaScript/reverse-a-string-2.js
Normal file
11
Task/Reverse-a-string/JavaScript/reverse-a-string-2.js
Normal file
|
|
@ -0,0 +1,11 @@
|
|||
a = "\u{1F466}\u{1F3FB}\u{1f44b}"; // '👦🏻👋'
|
||||
|
||||
// wrong behavior - ASCII sequences
|
||||
a.split('').reverse().join(''); // '\udc4b🁦\ud83d'
|
||||
|
||||
// wrong behavior - Unicode code points
|
||||
[...a].reverse().join(''); // '👋🏻👦'
|
||||
a.split(/(?:)/u).reverse().join(''); // '👋🏻👦'
|
||||
|
||||
// correct behavior - Unicode graphemes
|
||||
[...new Intl.Segmenter().segment(a)].map(x => x.segment).reverse().join('') // 👋👦🏻
|
||||
17
Task/Reverse-a-string/JavaScript/reverse-a-string-3.js
Normal file
17
Task/Reverse-a-string/JavaScript/reverse-a-string-3.js
Normal file
|
|
@ -0,0 +1,17 @@
|
|||
//using chained methods
|
||||
function reverseStr(s) {
|
||||
return s.split('').reverse().join('');
|
||||
}
|
||||
|
||||
//fast method using for loop
|
||||
function reverseStr(s) {
|
||||
for (var i = s.length - 1, o = ''; i >= 0; o += s[i--]) { }
|
||||
return o;
|
||||
}
|
||||
|
||||
//fast method using while loop (faster with long strings in some browsers when compared with for loop)
|
||||
function reverseStr(s) {
|
||||
var i = s.length, o = '';
|
||||
while (i--) o += s[i];
|
||||
return o;
|
||||
}
|
||||
18
Task/Reverse-a-string/JavaScript/reverse-a-string-4.js
Normal file
18
Task/Reverse-a-string/JavaScript/reverse-a-string-4.js
Normal file
|
|
@ -0,0 +1,18 @@
|
|||
(() => {
|
||||
|
||||
// .reduceRight() can be useful when reversals
|
||||
// are composed with some other process
|
||||
|
||||
let reverse1 = s => Array.from(s)
|
||||
.reduceRight((a, x) => a + (x !== ' ' ? x : ' <- '), ''),
|
||||
|
||||
// but ( join . reverse . split ) is faster for
|
||||
// simple string reversals in isolation
|
||||
|
||||
reverse2 = s => s.split('').reverse().join('');
|
||||
|
||||
|
||||
return [reverse1, reverse2]
|
||||
.map(f => f("Some string to be reversed"));
|
||||
|
||||
})();
|
||||
1
Task/Reverse-a-string/JavaScript/reverse-a-string-5.js
Normal file
1
Task/Reverse-a-string/JavaScript/reverse-a-string-5.js
Normal file
|
|
@ -0,0 +1 @@
|
|||
["desrever <- eb <- ot <- gnirts <- emoS", "desrever eb ot gnirts emoS"]
|
||||
Loading…
Add table
Add a link
Reference in a new issue