-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
56 lines (46 loc) · 1.56 KB
/
index.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
// Function: Adult?
const isAdult = num => num <= 0 ? false : num > 17;
// Function: Sort array numbers in ascending order
const sortArrayUp = arr => arr.sort((a, b) => a - b);
// Function: Sort array numbers in descending order
const sortArrayDown = arr => arr.sort((a, b) => b - a);
// Function: Palindrome?
const isPalindrome = str =>
!isNaN(str) ? false :
typeof str !== 'string' ? false :
!/[a-zA-Z]/.test(str) ? false :
(str = str.toLowerCase()) === str.split``.reverse().join ``;
// Function: Odd number?
const isOddNumber = num => typeof num !== 'number' || isNaN(num) ? false : num % 2 !== 0;
// Function: Even number?
const isEvenNumber = num => typeof num !== 'number' || isNaN(num) ? false : num % 2 === 0;
// Function: First letter Uppercase?
const isFirstLetterUpperCase = str =>
!isNaN(str) ? false :
typeof str !== 'string' ? false :
!/[a-zA-Z]/.test(str) ? false :
str[0] === str[0].toUpperCase();
// Function: First letter Lowercase?
const isFirsLetterLowerCase = str =>
!isNaN(str) ? false :
typeof str !== 'string' ? false :
!/[a-zA-Z]/.test(str) ? false :
str === str.toLowerCase();
// Function: Integer?
const isInteger = num => (num ^ 0) === num;
// Function: Replace spaces with underscore
const spaceToUnderscore = str =>
/[a-zA-Z]/.test(str[0]) && isNaN(str) ?
str.replace(/[' ']/g, '_') : false;
module.exports = {
isAdult,
sortArrayUp,
sortArrayDown,
isPalindrome,
isOddNumber,
isEvenNumber,
isFirstLetterUpperCase,
isFirsLetterLowerCase,
isInteger,
spaceToUnderscore
};