-
Notifications
You must be signed in to change notification settings - Fork 0
/
reverse.js
89 lines (78 loc) · 3.04 KB
/
reverse.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
const _run = require("./helper/_run");
function reverse1(value) {
function reverseCore(str) {
let reversedValue = '';
for (let i = str.length - 1; i >= 0; i--) {
reversedValue += str[i];
}
return reversedValue;
}
const isNumber = typeof value === "number";
const sign = Math.sign(value);
const isFloat = isNumber && value !== parseInt(value);
value = ((value) => isNumber && sign === -1 ? value.slice(1, value.length) : value)(value.toString());
let reversedValue;
if (isFloat) {
const splicedValue = value.split('.');
reversedValue = reverseCore(splicedValue[0]) + '.' + reverseCore(splicedValue[1]);
} else {
reversedValue = reverseCore(value);
}
return isNumber ? (+reversedValue) * sign : reversedValue;
}
function reverse2(value) {
function reverseCore(str) {
return [...str].reverse().join('');
}
const isNumber = typeof value === "number";
const sign = Math.sign(value);
const isFloat = isNumber && value !== parseInt(value);
value = ((value) => isNumber && sign === -1 ? value.slice(1, value.length) : value)(value.toString());
let reversedValue;
if (isFloat) {
const splicedValue = value.split('.');
reversedValue = reverseCore(splicedValue[0]) + '.' + reverseCore(splicedValue[1]);
} else {
reversedValue = reverseCore(value);
}
return isNumber ? (+reversedValue) * sign : reversedValue;
}
function reverse3(value) {
function reverseCore(str) {
return [...str].reduce((reversedString, currentChar) => currentChar + reversedString, '');
}
const isNumber = typeof value === "number";
const sign = Math.sign(value);
const isFloat = isNumber && value !== parseInt(value);
value = ((value) => isNumber && sign === -1 ? value.slice(1, value.length) : value)(value.toString());
let reversedValue;
if (isFloat) {
const splicedValue = value.split('.');
reversedValue = reverseCore(splicedValue[0]) + '.' + reverseCore(splicedValue[1]);
} else {
reversedValue = reverseCore(value);
}
return isNumber ? (+reversedValue) * sign : reversedValue;
}
function reverse4(value) {
function reverseCore(str) {
return [...str].map((_, index) => str[str.length - index - 1]).join('');
}
const isNumber = typeof value === "number";
const sign = Math.sign(value);
const isFloat = isNumber && value !== parseInt(value);
value = ((value) => isNumber && sign === -1 ? value.slice(1, value.length) : value)(value.toString());
let reversedValue;
if (isFloat) {
const splicedValue = value.split('.');
reversedValue = reverseCore(splicedValue[0]) + '.' + reverseCore(splicedValue[1]);
} else {
reversedValue = reverseCore(value);
}
return isNumber ? (+reversedValue) * sign : reversedValue;
}
/* REVERSE THE STRING OR NUMBER (INT + FLOAT) */
/* Args:-
arg1 => string / integer / float that need to be reversed
*/
module.exports = { reverse1, reverse2, reverse3, reverse4 }